dm: gpio: Refactor to prepare for live tree support
[oweals/u-boot.git] / include / asm-generic / gpio.h
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * Copyright (c) 2011, NVIDIA Corp. All rights reserved.
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #ifndef _ASM_GENERIC_GPIO_H_
8 #define _ASM_GENERIC_GPIO_H_
9
10 struct ofnode_phandle_args;
11
12 /*
13  * Generic GPIO API for U-Boot
14  *
15  * --
16  * NB: This is deprecated. Please use the driver model functions instead:
17  *
18  *    - gpio_request_by_name()
19  *    - dm_gpio_get_value() etc.
20  *
21  * For now we need a dm_ prefix on some functions to avoid name collision.
22  * --
23  *
24  * GPIOs are numbered from 0 to GPIO_COUNT-1 which value is defined
25  * by the SOC/architecture.
26  *
27  * Each GPIO can be an input or output. If an input then its value can
28  * be read as 0 or 1. If an output then its value can be set to 0 or 1.
29  * If you try to write an input then the value is undefined. If you try
30  * to read an output, barring something very unusual,  you will get
31  * back the value of the output that you previously set.
32  *
33  * In some cases the operation may fail, for example if the GPIO number
34  * is out of range, or the GPIO is not available because its pin is
35  * being used by another function. In that case, functions may return
36  * an error value of -1.
37  */
38
39 /**
40  * @deprecated  Please use driver model instead
41  * Request a GPIO. This should be called before any of the other functions
42  * are used on this GPIO.
43  *
44  * Note: With driver model, the label is allocated so there is no need for
45  * the caller to preserve it.
46  *
47  * @param gpio  GPIO number
48  * @param label User label for this GPIO
49  * @return 0 if ok, -1 on error
50  */
51 int gpio_request(unsigned gpio, const char *label);
52
53 /**
54  * @deprecated  Please use driver model instead
55  * Stop using the GPIO.  This function should not alter pin configuration.
56  *
57  * @param gpio  GPIO number
58  * @return 0 if ok, -1 on error
59  */
60 int gpio_free(unsigned gpio);
61
62 /**
63  * @deprecated  Please use driver model instead
64  * Make a GPIO an input.
65  *
66  * @param gpio  GPIO number
67  * @return 0 if ok, -1 on error
68  */
69 int gpio_direction_input(unsigned gpio);
70
71 /**
72  * @deprecated  Please use driver model instead
73  * Make a GPIO an output, and set its value.
74  *
75  * @param gpio  GPIO number
76  * @param value GPIO value (0 for low or 1 for high)
77  * @return 0 if ok, -1 on error
78  */
79 int gpio_direction_output(unsigned gpio, int value);
80
81 /**
82  * @deprecated  Please use driver model instead
83  * Get a GPIO's value. This will work whether the GPIO is an input
84  * or an output.
85  *
86  * @param gpio  GPIO number
87  * @return 0 if low, 1 if high, -1 on error
88  */
89 int gpio_get_value(unsigned gpio);
90
91 /**
92  * @deprecated  Please use driver model instead
93  * Set an output GPIO's value. The GPIO must already be an output or
94  * this function may have no effect.
95  *
96  * @param gpio  GPIO number
97  * @param value GPIO value (0 for low or 1 for high)
98  * @return 0 if ok, -1 on error
99  */
100 int gpio_set_value(unsigned gpio, int value);
101
102 /* State of a GPIO, as reported by get_function() */
103 enum gpio_func_t {
104         GPIOF_INPUT = 0,
105         GPIOF_OUTPUT,
106         GPIOF_UNUSED,           /* Not claimed */
107         GPIOF_UNKNOWN,          /* Not known */
108         GPIOF_FUNC,             /* Not used as a GPIO */
109
110         GPIOF_COUNT,
111 };
112
113 struct udevice;
114
115 struct gpio_desc {
116         struct udevice *dev;    /* Device, NULL for invalid GPIO */
117         unsigned long flags;
118 #define GPIOD_REQUESTED         (1 << 0)        /* Requested/claimed */
119 #define GPIOD_IS_OUT            (1 << 1)        /* GPIO is an output */
120 #define GPIOD_IS_IN             (1 << 2)        /* GPIO is an input */
121 #define GPIOD_ACTIVE_LOW        (1 << 3)        /* value has active low */
122 #define GPIOD_IS_OUT_ACTIVE     (1 << 4)        /* set output active */
123
124         uint offset;            /* GPIO offset within the device */
125         /*
126          * We could consider adding the GPIO label in here. Possibly we could
127          * use this structure for internal GPIO information.
128          */
129 };
130
131 /**
132  * dm_gpio_is_valid() - Check if a GPIO is valid
133  *
134  * @desc:       GPIO description containing device, offset and flags,
135  *              previously returned by gpio_request_by_name()
136  * @return true if valid, false if not
137  */
138 static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
139 {
140         return desc->dev != NULL;
141 }
142
143 /**
144  * gpio_get_status() - get the current GPIO status as a string
145  *
146  * Obtain the current GPIO status as a string which can be presented to the
147  * user. A typical string is:
148  *
149  * "b4:  in: 1 [x] sdmmc_cd"
150  *
151  * which means this is GPIO bank b, offset 4, currently set to input, current
152  * value 1, [x] means that it is requested and the owner is 'sdmmc_cd'
153  *
154  * TODO(sjg@chromium.org): This should use struct gpio_desc
155  *
156  * @dev:        Device to check
157  * @offset:     Offset of device GPIO to check
158  * @buf:        Place to put string
159  * @buffsize:   Size of string including \0
160  */
161 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize);
162
163 /**
164  * gpio_get_function() - get the current function for a GPIO pin
165  *
166  * Note this returns GPIOF_UNUSED if the GPIO is not requested.
167  *
168  * TODO(sjg@chromium.org): This should use struct gpio_desc
169  *
170  * @dev:        Device to check
171  * @offset:     Offset of device GPIO to check
172  * @namep:      If non-NULL, this is set to the name given when the GPIO
173  *              was requested, or -1 if it has not been requested
174  * @return  -ENODATA if the driver returned an unknown function,
175  * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
176  * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the
177  * function from enum gpio_func_t.
178  */
179 int gpio_get_function(struct udevice *dev, int offset, const char **namep);
180
181 /**
182  * gpio_get_raw_function() - get the current raw function for a GPIO pin
183  *
184  * Note this does not return GPIOF_UNUSED - it will always return the GPIO
185  * driver's view of a pin function, even if it is not correctly set up.
186  *
187  * TODO(sjg@chromium.org): This should use struct gpio_desc
188  *
189  * @dev:        Device to check
190  * @offset:     Offset of device GPIO to check
191  * @namep:      If non-NULL, this is set to the name given when the GPIO
192  *              was requested, or -1 if it has not been requested
193  * @return  -ENODATA if the driver returned an unknown function,
194  * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
195  * Otherwise returns the function from enum gpio_func_t.
196  */
197 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep);
198
199 /**
200  * gpio_requestf() - request a GPIO using a format string for the owner
201  *
202  * This is a helper function for gpio_request(). It allows you to provide
203  * a printf()-format string for the GPIO owner. It calls gpio_request() with
204  * the string that is created
205  */
206 int gpio_requestf(unsigned gpio, const char *fmt, ...)
207                 __attribute__ ((format (__printf__, 2, 3)));
208
209 struct fdtdec_phandle_args;
210
211 /**
212  * gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate
213  *
214  * This routine sets the offset field to args[0] and the flags field to
215  * GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1].
216  *
217  */
218 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
219                           struct ofnode_phandle_args *args);
220
221 /**
222  * struct struct dm_gpio_ops - Driver model GPIO operations
223  *
224  * Refer to functions above for description. These function largely copy
225  * the old API.
226  *
227  * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
228  * new DM GPIO API, this should be really easy to flip over to the Linux
229  * GPIO API-alike interface.
230  *
231  * Also it would be useful to standardise additional functions like
232  * pullup, slew rate and drive strength.
233  *
234  * gpio_request() and gpio_free() are optional - if NULL then they will
235  * not be called.
236  *
237  * Note that @offset is the offset from the base GPIO of the device. So
238  * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
239  * where o is the number of GPIO lines controlled by the device. A device
240  * is typically used to control a single bank of GPIOs. Within complex
241  * SoCs there may be many banks and therefore many devices all referring
242  * to the different IO addresses within the SoC.
243  *
244  * The uclass combines all GPIO devices together to provide a consistent
245  * numbering from 0 to n-1, where n is the number of GPIOs in total across
246  * all devices. Be careful not to confuse offset with gpio in the parameters.
247  */
248 struct dm_gpio_ops {
249         int (*request)(struct udevice *dev, unsigned offset, const char *label);
250         int (*free)(struct udevice *dev, unsigned offset);
251         int (*direction_input)(struct udevice *dev, unsigned offset);
252         int (*direction_output)(struct udevice *dev, unsigned offset,
253                                 int value);
254         int (*get_value)(struct udevice *dev, unsigned offset);
255         int (*set_value)(struct udevice *dev, unsigned offset, int value);
256         int (*get_open_drain)(struct udevice *dev, unsigned offset);
257         int (*set_open_drain)(struct udevice *dev, unsigned offset, int value);
258         /**
259          * get_function() Get the GPIO function
260          *
261          * @dev:     Device to check
262          * @offset:  GPIO offset within that device
263          * @return current function - GPIOF_...
264          */
265         int (*get_function)(struct udevice *dev, unsigned offset);
266
267         /**
268          * xlate() - Translate phandle arguments into a GPIO description
269          *
270          * This function should set up the fields in desc according to the
271          * information in the arguments. The uclass will have set up:
272          *
273          *   @desc->dev to @dev
274          *   @desc->flags to 0
275          *   @desc->offset to 0
276          *
277          * This method is optional and defaults to gpio_xlate_offs_flags,
278          * which will parse offset and the GPIO_ACTIVE_LOW flag in the first
279          * two arguments.
280          *
281          * Note that @dev is passed in as a parameter to follow driver model
282          * uclass conventions, even though it is already available as
283          * desc->dev.
284          *
285          * @dev:        GPIO device
286          * @desc:       Place to put GPIO description
287          * @args:       Arguments provided in description
288          * @return 0 if OK, -ve on error
289          */
290         int (*xlate)(struct udevice *dev, struct gpio_desc *desc,
291                      struct ofnode_phandle_args *args);
292 };
293
294 /**
295  * struct gpio_dev_priv - information about a device used by the uclass
296  *
297  * The uclass combines all active GPIO devices into a unified numbering
298  * scheme. To do this it maintains some private information about each
299  * device.
300  *
301  * To implement driver model support in your GPIO driver, add a probe
302  * handler, and set @gpio_count and @bank_name correctly in that handler.
303  * This tells the uclass the name of the GPIO bank and the number of GPIOs
304  * it contains.
305  *
306  * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
307  * 'A0', 'A1', etc.
308  * @gpio_count: Number of GPIOs in this device
309  * @gpio_base: Base GPIO number for this device. For the first active device
310  * this will be 0; the numbering for others will follow sequentially so that
311  * @gpio_base for device 1 will equal the number of GPIOs in device 0.
312  * @name: Array of pointers to the name for each GPIO in this bank. The
313  * value of the pointer will be NULL if the GPIO has not been claimed.
314  */
315 struct gpio_dev_priv {
316         const char *bank_name;
317         unsigned gpio_count;
318         unsigned gpio_base;
319         char **name;
320 };
321
322 /* Access the GPIO operations for a device */
323 #define gpio_get_ops(dev)       ((struct dm_gpio_ops *)(dev)->driver->ops)
324
325 /**
326  * gpio_get_bank_info - Return information about a GPIO bank/device
327  *
328  * This looks up a device and returns both its GPIO base name and the number
329  * of GPIOs it controls.
330  *
331  * @dev: Device to look up
332  * @offset_count: Returns number of GPIOs within this bank
333  * @return bank name of this device
334  */
335 const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
336
337 /**
338  * dm_gpio_lookup_name() - Look up a named GPIO and return its description
339  *
340  * The name of a GPIO is typically its bank name followed by a number from 0.
341  * For example A0 is the first GPIO in bank A. Each bank is a separate driver
342  * model device.
343  *
344  * @name:       Name to look up
345  * @desc:       Returns description, on success
346  * @return 0 if OK, -ve on error
347  */
348 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc);
349
350 /**
351  * gpio_lookup_name - Look up a GPIO name and return its details
352  *
353  * This is used to convert a named GPIO into a device, offset and GPIO
354  * number.
355  *
356  * @name: GPIO name to look up
357  * @devp: Returns pointer to device which contains this GPIO
358  * @offsetp: Returns the offset number within this device
359  * @gpiop: Returns the absolute GPIO number, numbered from 0
360  */
361 int gpio_lookup_name(const char *name, struct udevice **devp,
362                      unsigned int *offsetp, unsigned int *gpiop);
363
364 /**
365  * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
366  *
367  * This puts the value of the first GPIO into bit 0, the second into bit 1,
368  * etc. then returns the resulting integer.
369  *
370  * @gpio_list: List of GPIOs to collect
371  * @return resulting integer value, or -ve on error
372  */
373 int gpio_get_values_as_int(const int *gpio_list);
374
375 /**
376  * dm_gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
377  *
378  * This puts the value of the first GPIO into bit 0, the second into bit 1,
379  * etc. then returns the resulting integer.
380  *
381  * @desc_list: List of GPIOs to collect
382  * @count: Number of GPIOs
383  * @return resulting integer value, or -ve on error
384  */
385 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count);
386
387 /**
388  * gpio_claim_vector() - claim a number of GPIOs for input
389  *
390  * @gpio_num_array:     array of gpios to claim, terminated by -1
391  * @fmt:                format string for GPIO names, e.g. "board_id%d"
392  * @return 0 if OK, -ve on error
393  */
394 int gpio_claim_vector(const int *gpio_num_array, const char *fmt);
395
396 /**
397  * gpio_request_by_name() - Locate and request a GPIO by name
398  *
399  * This operates by looking up the given list name in the device (device
400  * tree property) and requesting the GPIO for use. The property must exist
401  * in @dev's node.
402  *
403  * Use @flags to specify whether the GPIO should be an input or output. In
404  * principle this can also come from the device tree binding but most
405  * bindings don't provide this information. Specifically, when the GPIO uclass
406  * calls the xlate() method, it can return default flags, which are then
407  * ORed with this @flags.
408  *
409  * If we find that requesting the GPIO is not always needed we could add a
410  * new function or a new GPIOD_NO_REQUEST flag.
411  *
412  * At present driver model has no reference counting so if one device
413  * requests a GPIO which subsequently is unbound, the @desc->dev pointer
414  * will be invalid. However this will only happen if the GPIO device is
415  * unbound, not if it is removed, so this seems like a reasonable limitation
416  * for now. There is no real use case for unbinding drivers in normal
417  * operation.
418  *
419  * The device tree binding is doc/device-tree-bindings/gpio/gpio.txt in
420  * generate terms and each specific device may add additional details in
421  * a binding file in the same directory.
422  *
423  * @dev:        Device requesting the GPIO
424  * @list_name:  Name of GPIO list (e.g. "board-id-gpios")
425  * @index:      Index number of the GPIO in that list use request (0=first)
426  * @desc:       Returns GPIO description information. If there is no such
427  *              GPIO, dev->dev will be NULL.
428  * @flags:      Indicates the GPIO input/output settings (GPIOD_...)
429  * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is
430  * something wrong with the list, or other -ve for another error (e.g.
431  * -EBUSY if a GPIO was already requested)
432  */
433 int gpio_request_by_name(struct udevice *dev, const char *list_name,
434                          int index, struct gpio_desc *desc, int flags);
435
436 /**
437  * gpio_request_list_by_name() - Request a list of GPIOs
438  *
439  * Reads all the GPIOs from a list and requests them. See
440  * gpio_request_by_name() for additional details. Lists should not be
441  * misused to hold unrelated or optional GPIOs. They should only be used
442  * for things like parallel data lines. A zero phandle terminates the list
443  * the list.
444  *
445  * This function will either succeed, and request all GPIOs in the list, or
446  * fail and request none (it will free already-requested GPIOs in case of
447  * an error part-way through).
448  *
449  * @dev:        Device requesting the GPIO
450  * @list_name:  Name of GPIO list (e.g. "board-id-gpios")
451  * @desc_list:  Returns a list of GPIO description information
452  * @max_count:  Maximum number of GPIOs to return (@desc_list must be at least
453  *              this big)
454  * @flags:      Indicates the GPIO input/output settings (GPIOD_...)
455  * @return number of GPIOs requested, or -ve on error
456  */
457 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
458                               struct gpio_desc *desc_list, int max_count,
459                               int flags);
460
461 /**
462  * dm_gpio_request() - manually request a GPIO
463  *
464  * Note: This function should only be used for testing / debugging. Instead.
465  * use gpio_request_by_name() to pull GPIOs from the device tree.
466  *
467  * @desc:       GPIO description of GPIO to request (see dm_gpio_lookup_name())
468  * @label:      Label to attach to the GPIO while claimed
469  * @return 0 if OK, -ve on error
470  */
471 int dm_gpio_request(struct gpio_desc *desc, const char *label);
472
473 /**
474  * gpio_get_list_count() - Returns the number of GPIOs in a list
475  *
476  * Counts the GPIOs in a list. See gpio_request_by_name() for additional
477  * details.
478  *
479  * @dev:        Device requesting the GPIO
480  * @list_name:  Name of GPIO list (e.g. "board-id-gpios")
481  * @return number of GPIOs (0 for an empty property) or -ENOENT if the list
482  * does not exist
483  */
484 int gpio_get_list_count(struct udevice *dev, const char *list_name);
485
486 /**
487  * gpio_request_by_name_nodev() - request GPIOs without a device
488  *
489  * This is a version of gpio_request_list_by_name() that does not use a
490  * device. Avoid it unless the caller is not yet using driver model
491  */
492 int gpio_request_by_name_nodev(const void *blob, int node,
493                                const char *list_name,
494                                int index, struct gpio_desc *desc, int flags);
495
496 /**
497  * gpio_request_list_by_name_nodev() - request GPIOs without a device
498  *
499  * This is a version of gpio_request_list_by_name() that does not use a
500  * device. Avoid it unless the caller is not yet using driver model
501  */
502 int gpio_request_list_by_name_nodev(const void *blob, int node,
503                                     const char *list_name,
504                                     struct gpio_desc *desc_list, int max_count,
505                                     int flags);
506
507 /**
508  * dm_gpio_free() - Free a single GPIO
509  *
510  * This frees a single GPIOs previously returned from gpio_request_by_name().
511  *
512  * @dev:        Device which requested the GPIO
513  * @desc:       GPIO to free
514  * @return 0 if OK, -ve on error
515  */
516 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc);
517
518 /**
519  * gpio_free_list() - Free a list of GPIOs
520  *
521  * This frees a list of GPIOs previously returned from
522  * gpio_request_list_by_name().
523  *
524  * @dev:        Device which requested the GPIOs
525  * @desc:       List of GPIOs to free
526  * @count:      Number of GPIOs in the list
527  * @return 0 if OK, -ve on error
528  */
529 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count);
530
531 /**
532  * gpio_free_list_nodev() - free GPIOs without a device
533  *
534  * This is a version of gpio_free_list() that does not use a
535  * device. Avoid it unless the caller is not yet using driver model
536  */
537 int gpio_free_list_nodev(struct gpio_desc *desc, int count);
538
539 /**
540  * dm_gpio_get_value() - Get the value of a GPIO
541  *
542  * This is the driver model version of the existing gpio_get_value() function
543  * and should be used instead of that.
544  *
545  * For now, these functions have a dm_ prefix since they conflict with
546  * existing names.
547  *
548  * @desc:       GPIO description containing device, offset and flags,
549  *              previously returned by gpio_request_by_name()
550  * @return GPIO value (0 for inactive, 1 for active) or -ve on error
551  */
552 int dm_gpio_get_value(const struct gpio_desc *desc);
553
554 int dm_gpio_set_value(const struct gpio_desc *desc, int value);
555
556 /**
557  * dm_gpio_get_open_drain() - Check if open-drain-mode of a GPIO is active
558  *
559  * This checks if open-drain-mode for a GPIO is enabled or not. This method is
560  * optional.
561  *
562  * @desc:       GPIO description containing device, offset and flags,
563  *              previously returned by gpio_request_by_name()
564  * @return Value of open drain mode for GPIO (0 for inactive, 1 for active) or
565  *         -ve on error
566  */
567 int dm_gpio_get_open_drain(struct gpio_desc *desc);
568
569 /**
570  * dm_gpio_set_open_drain() - Switch open-drain-mode of a GPIO on or off
571  *
572  * This enables or disables open-drain mode for a GPIO. This method is
573  * optional; if the driver does not support it, nothing happens when the method
574  * is called.
575  *
576  * In open-drain mode, instead of actively driving the output (Push-pull
577  * output), the GPIO's pin is connected to the collector (for a NPN transistor)
578  * or the drain (for a MOSFET) of a transistor, respectively. The pin then
579  * either forms an open circuit or a connection to ground, depending on the
580  * state of the transistor.
581  *
582  * @desc:       GPIO description containing device, offset and flags,
583  *              previously returned by gpio_request_by_name()
584  * @return 0 if OK, -ve on error
585  */
586 int dm_gpio_set_open_drain(struct gpio_desc *desc, int value);
587
588 /**
589  * dm_gpio_set_dir() - Set the direction for a GPIO
590  *
591  * This sets up the direction according tot the provided flags. It will do
592  * nothing unless the direction is actually specified.
593  *
594  * @desc:       GPIO description containing device, offset and flags,
595  *              previously returned by gpio_request_by_name()
596  * @return 0 if OK, -ve on error
597  */
598 int dm_gpio_set_dir(struct gpio_desc *desc);
599
600 /**
601  * dm_gpio_set_dir_flags() - Set direction using specific flags
602  *
603  * This is like dm_gpio_set_dir() except that the flags value is provided
604  * instead of being used from desc->flags. This is needed because in many
605  * cases the GPIO description does not include direction information.
606  * Note that desc->flags is updated by this function.
607  *
608  * @desc:       GPIO description containing device, offset and flags,
609  *              previously returned by gpio_request_by_name()
610  * @flags:      New flags to use
611  * @return 0 if OK, -ve on error, in which case desc->flags is not updated
612  */
613 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags);
614
615 /**
616  * gpio_get_number() - Get the global GPIO number of a GPIO
617  *
618  * This should only be used for debugging or interest. It returns the number
619  * that should be used for gpio_get_value() etc. to access this GPIO.
620  *
621  * @desc:       GPIO description containing device, offset and flags,
622  *              previously returned by gpio_request_by_name()
623  * @return GPIO number, or -ve if not found
624  */
625 int gpio_get_number(const struct gpio_desc *desc);
626
627 #endif  /* _ASM_GENERIC_GPIO_H_ */