driver.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * driver.h -- SoC Regulator driver support.
  3. *
  4. * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Regulator Driver Interface.
  13. */
  14. #ifndef __LINUX_REGULATOR_DRIVER_H_
  15. #define __LINUX_REGULATOR_DRIVER_H_
  16. #define MAX_COUPLED 4
  17. #include <linux/device.h>
  18. #include <linux/notifier.h>
  19. #include <linux/regulator/consumer.h>
  20. struct gpio_desc;
  21. struct regmap;
  22. struct regulator_dev;
  23. struct regulator_config;
  24. struct regulator_init_data;
  25. struct regulator_enable_gpio;
  26. enum regulator_status {
  27. REGULATOR_STATUS_OFF,
  28. REGULATOR_STATUS_ON,
  29. REGULATOR_STATUS_ERROR,
  30. /* fast/normal/idle/standby are flavors of "on" */
  31. REGULATOR_STATUS_FAST,
  32. REGULATOR_STATUS_NORMAL,
  33. REGULATOR_STATUS_IDLE,
  34. REGULATOR_STATUS_STANDBY,
  35. /* The regulator is enabled but not regulating */
  36. REGULATOR_STATUS_BYPASS,
  37. /* in case that any other status doesn't apply */
  38. REGULATOR_STATUS_UNDEFINED,
  39. };
  40. /**
  41. * struct regulator_linear_range - specify linear voltage ranges
  42. *
  43. * Specify a range of voltages for regulator_map_linear_range() and
  44. * regulator_list_linear_range().
  45. *
  46. * @min_uV: Lowest voltage in range
  47. * @min_sel: Lowest selector for range
  48. * @max_sel: Highest selector for range
  49. * @uV_step: Step size
  50. */
  51. struct regulator_linear_range {
  52. unsigned int min_uV;
  53. unsigned int min_sel;
  54. unsigned int max_sel;
  55. unsigned int uV_step;
  56. };
  57. /* Initialize struct regulator_linear_range */
  58. #define REGULATOR_LINEAR_RANGE(_min_uV, _min_sel, _max_sel, _step_uV) \
  59. { \
  60. .min_uV = _min_uV, \
  61. .min_sel = _min_sel, \
  62. .max_sel = _max_sel, \
  63. .uV_step = _step_uV, \
  64. }
  65. /**
  66. * struct regulator_ops - regulator operations.
  67. *
  68. * @enable: Configure the regulator as enabled.
  69. * @disable: Configure the regulator as disabled.
  70. * @is_enabled: Return 1 if the regulator is enabled, 0 if not.
  71. * May also return negative errno.
  72. *
  73. * @set_voltage: Set the voltage for the regulator within the range specified.
  74. * The driver should select the voltage closest to min_uV.
  75. * @set_voltage_sel: Set the voltage for the regulator using the specified
  76. * selector.
  77. * @map_voltage: Convert a voltage into a selector
  78. * @get_voltage: Return the currently configured voltage for the regulator;
  79. * return -ENOTRECOVERABLE if regulator can't be read at
  80. * bootup and hasn't been set yet.
  81. * @get_voltage_sel: Return the currently configured voltage selector for the
  82. * regulator; return -ENOTRECOVERABLE if regulator can't
  83. * be read at bootup and hasn't been set yet.
  84. * @list_voltage: Return one of the supported voltages, in microvolts; zero
  85. * if the selector indicates a voltage that is unusable on this system;
  86. * or negative errno. Selectors range from zero to one less than
  87. * regulator_desc.n_voltages. Voltages may be reported in any order.
  88. *
  89. * @set_current_limit: Configure a limit for a current-limited regulator.
  90. * The driver should select the current closest to max_uA.
  91. * @get_current_limit: Get the configured limit for a current-limited regulator.
  92. * @set_input_current_limit: Configure an input limit.
  93. *
  94. * @set_over_current_protection: Support capability of automatically shutting
  95. * down when detecting an over current event.
  96. *
  97. * @set_active_discharge: Set active discharge enable/disable of regulators.
  98. *
  99. * @set_mode: Set the configured operating mode for the regulator.
  100. * @get_mode: Get the configured operating mode for the regulator.
  101. * @get_error_flags: Get the current error(s) for the regulator.
  102. * @get_status: Return actual (not as-configured) status of regulator, as a
  103. * REGULATOR_STATUS value (or negative errno)
  104. * @get_optimum_mode: Get the most efficient operating mode for the regulator
  105. * when running with the specified parameters.
  106. * @set_load: Set the load for the regulator.
  107. *
  108. * @set_bypass: Set the regulator in bypass mode.
  109. * @get_bypass: Get the regulator bypass mode state.
  110. *
  111. * @enable_time: Time taken for the regulator voltage output voltage to
  112. * stabilise after being enabled, in microseconds.
  113. * @set_ramp_delay: Set the ramp delay for the regulator. The driver should
  114. * select ramp delay equal to or less than(closest) ramp_delay.
  115. * @set_voltage_time: Time taken for the regulator voltage output voltage
  116. * to stabilise after being set to a new value, in microseconds.
  117. * The function receives the from and to voltage as input, it
  118. * should return the worst case.
  119. * @set_voltage_time_sel: Time taken for the regulator voltage output voltage
  120. * to stabilise after being set to a new value, in microseconds.
  121. * The function receives the from and to voltage selector as
  122. * input, it should return the worst case.
  123. * @set_soft_start: Enable soft start for the regulator.
  124. *
  125. * @set_suspend_voltage: Set the voltage for the regulator when the system
  126. * is suspended.
  127. * @set_suspend_enable: Mark the regulator as enabled when the system is
  128. * suspended.
  129. * @set_suspend_disable: Mark the regulator as disabled when the system is
  130. * suspended.
  131. * @set_suspend_mode: Set the operating mode for the regulator when the
  132. * system is suspended.
  133. *
  134. * @set_pull_down: Configure the regulator to pull down when the regulator
  135. * is disabled.
  136. *
  137. * This struct describes regulator operations which can be implemented by
  138. * regulator chip drivers.
  139. */
  140. struct regulator_ops {
  141. /* enumerate supported voltages */
  142. int (*list_voltage) (struct regulator_dev *, unsigned selector);
  143. /* get/set regulator voltage */
  144. int (*set_voltage) (struct regulator_dev *, int min_uV, int max_uV,
  145. unsigned *selector);
  146. int (*map_voltage)(struct regulator_dev *, int min_uV, int max_uV);
  147. int (*set_voltage_sel) (struct regulator_dev *, unsigned selector);
  148. int (*get_voltage) (struct regulator_dev *);
  149. int (*get_voltage_sel) (struct regulator_dev *);
  150. /* get/set regulator current */
  151. int (*set_current_limit) (struct regulator_dev *,
  152. int min_uA, int max_uA);
  153. int (*get_current_limit) (struct regulator_dev *);
  154. int (*set_input_current_limit) (struct regulator_dev *, int lim_uA);
  155. int (*set_over_current_protection) (struct regulator_dev *);
  156. int (*set_active_discharge) (struct regulator_dev *, bool enable);
  157. /* enable/disable regulator */
  158. int (*enable) (struct regulator_dev *);
  159. int (*disable) (struct regulator_dev *);
  160. int (*is_enabled) (struct regulator_dev *);
  161. /* get/set regulator operating mode (defined in consumer.h) */
  162. int (*set_mode) (struct regulator_dev *, unsigned int mode);
  163. unsigned int (*get_mode) (struct regulator_dev *);
  164. /* retrieve current error flags on the regulator */
  165. int (*get_error_flags)(struct regulator_dev *, unsigned int *flags);
  166. /* Time taken to enable or set voltage on the regulator */
  167. int (*enable_time) (struct regulator_dev *);
  168. int (*set_ramp_delay) (struct regulator_dev *, int ramp_delay);
  169. int (*set_voltage_time) (struct regulator_dev *, int old_uV,
  170. int new_uV);
  171. int (*set_voltage_time_sel) (struct regulator_dev *,
  172. unsigned int old_selector,
  173. unsigned int new_selector);
  174. int (*set_soft_start) (struct regulator_dev *);
  175. /* report regulator status ... most other accessors report
  176. * control inputs, this reports results of combining inputs
  177. * from Linux (and other sources) with the actual load.
  178. * returns REGULATOR_STATUS_* or negative errno.
  179. */
  180. int (*get_status)(struct regulator_dev *);
  181. /* get most efficient regulator operating mode for load */
  182. unsigned int (*get_optimum_mode) (struct regulator_dev *, int input_uV,
  183. int output_uV, int load_uA);
  184. /* set the load on the regulator */
  185. int (*set_load)(struct regulator_dev *, int load_uA);
  186. /* control and report on bypass mode */
  187. int (*set_bypass)(struct regulator_dev *dev, bool enable);
  188. int (*get_bypass)(struct regulator_dev *dev, bool *enable);
  189. /* the operations below are for configuration of regulator state when
  190. * its parent PMIC enters a global STANDBY/HIBERNATE state */
  191. /* set regulator suspend voltage */
  192. int (*set_suspend_voltage) (struct regulator_dev *, int uV);
  193. /* enable/disable regulator in suspend state */
  194. int (*set_suspend_enable) (struct regulator_dev *);
  195. int (*set_suspend_disable) (struct regulator_dev *);
  196. /* set regulator suspend operating mode (defined in consumer.h) */
  197. int (*set_suspend_mode) (struct regulator_dev *, unsigned int mode);
  198. int (*resume)(struct regulator_dev *rdev);
  199. int (*set_pull_down) (struct regulator_dev *);
  200. };
  201. /*
  202. * Regulators can either control voltage or current.
  203. */
  204. enum regulator_type {
  205. REGULATOR_VOLTAGE,
  206. REGULATOR_CURRENT,
  207. };
  208. /**
  209. * struct regulator_desc - Static regulator descriptor
  210. *
  211. * Each regulator registered with the core is described with a
  212. * structure of this type and a struct regulator_config. This
  213. * structure contains the non-varying parts of the regulator
  214. * description.
  215. *
  216. * @name: Identifying name for the regulator.
  217. * @supply_name: Identifying the regulator supply
  218. * @of_match: Name used to identify regulator in DT.
  219. * @regulators_node: Name of node containing regulator definitions in DT.
  220. * @of_parse_cb: Optional callback called only if of_match is present.
  221. * Will be called for each regulator parsed from DT, during
  222. * init_data parsing.
  223. * The regulator_config passed as argument to the callback will
  224. * be a copy of config passed to regulator_register, valid only
  225. * for this particular call. Callback may freely change the
  226. * config but it cannot store it for later usage.
  227. * Callback should return 0 on success or negative ERRNO
  228. * indicating failure.
  229. * @id: Numerical identifier for the regulator.
  230. * @ops: Regulator operations table.
  231. * @irq: Interrupt number for the regulator.
  232. * @type: Indicates if the regulator is a voltage or current regulator.
  233. * @owner: Module providing the regulator, used for refcounting.
  234. *
  235. * @continuous_voltage_range: Indicates if the regulator can set any
  236. * voltage within constrains range.
  237. * @n_voltages: Number of selectors available for ops.list_voltage().
  238. *
  239. * @min_uV: Voltage given by the lowest selector (if linear mapping)
  240. * @uV_step: Voltage increase with each selector (if linear mapping)
  241. * @linear_min_sel: Minimal selector for starting linear mapping
  242. * @fixed_uV: Fixed voltage of rails.
  243. * @ramp_delay: Time to settle down after voltage change (unit: uV/us)
  244. * @min_dropout_uV: The minimum dropout voltage this regulator can handle
  245. * @linear_ranges: A constant table of possible voltage ranges.
  246. * @linear_range_selectors: A constant table of voltage range selectors.
  247. * If pickable ranges are used each range must
  248. * have corresponding selector here.
  249. * @n_linear_ranges: Number of entries in the @linear_ranges (and in
  250. * linear_range_selectors if used) table(s).
  251. * @volt_table: Voltage mapping table (if table based mapping)
  252. *
  253. * @vsel_range_reg: Register for range selector when using pickable ranges
  254. * and regulator_regmap_X_voltage_X_pickable functions.
  255. * @vsel_range_mask: Mask for register bitfield used for range selector
  256. * @vsel_reg: Register for selector when using regulator_regmap_X_voltage_
  257. * @vsel_mask: Mask for register bitfield used for selector
  258. * @csel_reg: Register for TPS65218 LS3 current regulator
  259. * @csel_mask: Mask for TPS65218 LS3 current regulator
  260. * @apply_reg: Register for initiate voltage change on the output when
  261. * using regulator_set_voltage_sel_regmap
  262. * @apply_bit: Register bitfield used for initiate voltage change on the
  263. * output when using regulator_set_voltage_sel_regmap
  264. * @enable_reg: Register for control when using regmap enable/disable ops
  265. * @enable_mask: Mask for control when using regmap enable/disable ops
  266. * @enable_val: Enabling value for control when using regmap enable/disable ops
  267. * @disable_val: Disabling value for control when using regmap enable/disable ops
  268. * @enable_is_inverted: A flag to indicate set enable_mask bits to disable
  269. * when using regulator_enable_regmap and friends APIs.
  270. * @bypass_reg: Register for control when using regmap set_bypass
  271. * @bypass_mask: Mask for control when using regmap set_bypass
  272. * @bypass_val_on: Enabling value for control when using regmap set_bypass
  273. * @bypass_val_off: Disabling value for control when using regmap set_bypass
  274. * @active_discharge_off: Enabling value for control when using regmap
  275. * set_active_discharge
  276. * @active_discharge_on: Disabling value for control when using regmap
  277. * set_active_discharge
  278. * @active_discharge_mask: Mask for control when using regmap
  279. * set_active_discharge
  280. * @active_discharge_reg: Register for control when using regmap
  281. * set_active_discharge
  282. * @soft_start_reg: Register for control when using regmap set_soft_start
  283. * @soft_start_mask: Mask for control when using regmap set_soft_start
  284. * @soft_start_val_on: Enabling value for control when using regmap
  285. * set_soft_start
  286. * @pull_down_reg: Register for control when using regmap set_pull_down
  287. * @pull_down_mask: Mask for control when using regmap set_pull_down
  288. * @pull_down_val_on: Enabling value for control when using regmap
  289. * set_pull_down
  290. *
  291. * @enable_time: Time taken for initial enable of regulator (in uS).
  292. * @off_on_delay: guard time (in uS), before re-enabling a regulator
  293. *
  294. * @of_map_mode: Maps a hardware mode defined in a DeviceTree to a standard mode
  295. */
  296. struct regulator_desc {
  297. const char *name;
  298. const char *supply_name;
  299. const char *of_match;
  300. const char *regulators_node;
  301. int (*of_parse_cb)(struct device_node *,
  302. const struct regulator_desc *,
  303. struct regulator_config *);
  304. int id;
  305. unsigned int continuous_voltage_range:1;
  306. unsigned n_voltages;
  307. const struct regulator_ops *ops;
  308. int irq;
  309. enum regulator_type type;
  310. struct module *owner;
  311. unsigned int min_uV;
  312. unsigned int uV_step;
  313. unsigned int linear_min_sel;
  314. int fixed_uV;
  315. unsigned int ramp_delay;
  316. int min_dropout_uV;
  317. const struct regulator_linear_range *linear_ranges;
  318. const unsigned int *linear_range_selectors;
  319. int n_linear_ranges;
  320. const unsigned int *volt_table;
  321. unsigned int vsel_range_reg;
  322. unsigned int vsel_range_mask;
  323. unsigned int vsel_reg;
  324. unsigned int vsel_mask;
  325. unsigned int csel_reg;
  326. unsigned int csel_mask;
  327. unsigned int apply_reg;
  328. unsigned int apply_bit;
  329. unsigned int enable_reg;
  330. unsigned int enable_mask;
  331. unsigned int enable_val;
  332. unsigned int disable_val;
  333. bool enable_is_inverted;
  334. unsigned int bypass_reg;
  335. unsigned int bypass_mask;
  336. unsigned int bypass_val_on;
  337. unsigned int bypass_val_off;
  338. unsigned int active_discharge_on;
  339. unsigned int active_discharge_off;
  340. unsigned int active_discharge_mask;
  341. unsigned int active_discharge_reg;
  342. unsigned int soft_start_reg;
  343. unsigned int soft_start_mask;
  344. unsigned int soft_start_val_on;
  345. unsigned int pull_down_reg;
  346. unsigned int pull_down_mask;
  347. unsigned int pull_down_val_on;
  348. unsigned int enable_time;
  349. unsigned int off_on_delay;
  350. unsigned int (*of_map_mode)(unsigned int mode);
  351. };
  352. /**
  353. * struct regulator_config - Dynamic regulator descriptor
  354. *
  355. * Each regulator registered with the core is described with a
  356. * structure of this type and a struct regulator_desc. This structure
  357. * contains the runtime variable parts of the regulator description.
  358. *
  359. * @dev: struct device for the regulator
  360. * @init_data: platform provided init data, passed through by driver
  361. * @driver_data: private regulator data
  362. * @of_node: OpenFirmware node to parse for device tree bindings (may be
  363. * NULL).
  364. * @regmap: regmap to use for core regmap helpers if dev_get_regmap() is
  365. * insufficient.
  366. * @ena_gpio_initialized: GPIO controlling regulator enable was properly
  367. * initialized, meaning that >= 0 is a valid gpio
  368. * identifier and < 0 is a non existent gpio.
  369. * @ena_gpio: GPIO controlling regulator enable.
  370. * @ena_gpiod: GPIO descriptor controlling regulator enable.
  371. * @ena_gpio_invert: Sense for GPIO enable control.
  372. * @ena_gpio_flags: Flags to use when calling gpio_request_one()
  373. */
  374. struct regulator_config {
  375. struct device *dev;
  376. const struct regulator_init_data *init_data;
  377. void *driver_data;
  378. struct device_node *of_node;
  379. struct regmap *regmap;
  380. bool ena_gpio_initialized;
  381. int ena_gpio;
  382. struct gpio_desc *ena_gpiod;
  383. unsigned int ena_gpio_invert:1;
  384. unsigned int ena_gpio_flags;
  385. };
  386. /*
  387. * struct coupling_desc
  388. *
  389. * Describes coupling of regulators. Each regulator should have
  390. * at least a pointer to itself in coupled_rdevs array.
  391. * When a new coupled regulator is resolved, n_resolved is
  392. * incremented.
  393. */
  394. struct coupling_desc {
  395. struct regulator_dev *coupled_rdevs[MAX_COUPLED];
  396. int n_resolved;
  397. int n_coupled;
  398. };
  399. /*
  400. * struct regulator_dev
  401. *
  402. * Voltage / Current regulator class device. One for each
  403. * regulator.
  404. *
  405. * This should *not* be used directly by anything except the regulator
  406. * core and notification injection (which should take the mutex and do
  407. * no other direct access).
  408. */
  409. struct regulator_dev {
  410. const struct regulator_desc *desc;
  411. int exclusive;
  412. u32 use_count;
  413. u32 open_count;
  414. u32 bypass_count;
  415. /* lists we belong to */
  416. struct list_head list; /* list of all regulators */
  417. /* lists we own */
  418. struct list_head consumer_list; /* consumers we supply */
  419. struct coupling_desc coupling_desc;
  420. struct blocking_notifier_head notifier;
  421. struct mutex mutex; /* consumer lock */
  422. struct task_struct *mutex_owner;
  423. int ref_cnt;
  424. struct module *owner;
  425. struct device dev;
  426. struct regulation_constraints *constraints;
  427. struct regulator *supply; /* for tree */
  428. const char *supply_name;
  429. struct regmap *regmap;
  430. struct delayed_work disable_work;
  431. int deferred_disables;
  432. void *reg_data; /* regulator_dev data */
  433. struct dentry *debugfs;
  434. struct regulator_enable_gpio *ena_pin;
  435. unsigned int ena_gpio_state:1;
  436. unsigned int is_switch:1;
  437. /* time when this regulator was disabled last time */
  438. unsigned long last_off_jiffy;
  439. };
  440. struct regulator_dev *
  441. regulator_register(const struct regulator_desc *regulator_desc,
  442. const struct regulator_config *config);
  443. struct regulator_dev *
  444. devm_regulator_register(struct device *dev,
  445. const struct regulator_desc *regulator_desc,
  446. const struct regulator_config *config);
  447. void regulator_unregister(struct regulator_dev *rdev);
  448. void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev);
  449. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  450. unsigned long event, void *data);
  451. void *rdev_get_drvdata(struct regulator_dev *rdev);
  452. struct device *rdev_get_dev(struct regulator_dev *rdev);
  453. int rdev_get_id(struct regulator_dev *rdev);
  454. int regulator_mode_to_status(unsigned int);
  455. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  456. unsigned int selector);
  457. int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
  458. unsigned int selector);
  459. int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
  460. unsigned int selector);
  461. int regulator_list_voltage_table(struct regulator_dev *rdev,
  462. unsigned int selector);
  463. int regulator_map_voltage_linear(struct regulator_dev *rdev,
  464. int min_uV, int max_uV);
  465. int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
  466. int min_uV, int max_uV);
  467. int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
  468. int min_uV, int max_uV);
  469. int regulator_map_voltage_iterate(struct regulator_dev *rdev,
  470. int min_uV, int max_uV);
  471. int regulator_map_voltage_ascend(struct regulator_dev *rdev,
  472. int min_uV, int max_uV);
  473. int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev);
  474. int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
  475. unsigned int sel);
  476. int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev);
  477. int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel);
  478. int regulator_is_enabled_regmap(struct regulator_dev *rdev);
  479. int regulator_enable_regmap(struct regulator_dev *rdev);
  480. int regulator_disable_regmap(struct regulator_dev *rdev);
  481. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  482. unsigned int old_selector,
  483. unsigned int new_selector);
  484. int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable);
  485. int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable);
  486. int regulator_set_soft_start_regmap(struct regulator_dev *rdev);
  487. int regulator_set_pull_down_regmap(struct regulator_dev *rdev);
  488. int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
  489. bool enable);
  490. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data);
  491. #endif