pv88060-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * pv88060-regulator.c - Regulator device driver for PV88060
  3. * Copyright (C) 2015 Powerventure Semiconductor Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/regmap.h>
  23. #include <linux/irq.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/regulator/of_regulator.h>
  26. #include "pv88060-regulator.h"
  27. #define PV88060_MAX_REGULATORS 14
  28. /* PV88060 REGULATOR IDs */
  29. enum {
  30. /* BUCKs */
  31. PV88060_ID_BUCK1,
  32. /* LDOs */
  33. PV88060_ID_LDO1,
  34. PV88060_ID_LDO2,
  35. PV88060_ID_LDO3,
  36. PV88060_ID_LDO4,
  37. PV88060_ID_LDO5,
  38. PV88060_ID_LDO6,
  39. PV88060_ID_LDO7,
  40. /* SWTs */
  41. PV88060_ID_SW1,
  42. PV88060_ID_SW2,
  43. PV88060_ID_SW3,
  44. PV88060_ID_SW4,
  45. PV88060_ID_SW5,
  46. PV88060_ID_SW6,
  47. };
  48. struct pv88060_regulator {
  49. struct regulator_desc desc;
  50. /* Current limiting */
  51. unsigned n_current_limits;
  52. const int *current_limits;
  53. unsigned int limit_mask;
  54. unsigned int conf; /* buck configuration register */
  55. };
  56. struct pv88060 {
  57. struct device *dev;
  58. struct regmap *regmap;
  59. struct regulator_dev *rdev[PV88060_MAX_REGULATORS];
  60. };
  61. static const struct regmap_config pv88060_regmap_config = {
  62. .reg_bits = 8,
  63. .val_bits = 8,
  64. };
  65. /* Current limits array (in uA) for BUCK1
  66. * Entry indexes corresponds to register values.
  67. */
  68. static const int pv88060_buck1_limits[] = {
  69. 1496000, 2393000, 3291000, 4189000
  70. };
  71. static unsigned int pv88060_buck_get_mode(struct regulator_dev *rdev)
  72. {
  73. struct pv88060_regulator *info = rdev_get_drvdata(rdev);
  74. unsigned int data;
  75. int ret, mode = 0;
  76. ret = regmap_read(rdev->regmap, info->conf, &data);
  77. if (ret < 0)
  78. return ret;
  79. switch (data & PV88060_BUCK_MODE_MASK) {
  80. case PV88060_BUCK_MODE_SYNC:
  81. mode = REGULATOR_MODE_FAST;
  82. break;
  83. case PV88060_BUCK_MODE_AUTO:
  84. mode = REGULATOR_MODE_NORMAL;
  85. break;
  86. case PV88060_BUCK_MODE_SLEEP:
  87. mode = REGULATOR_MODE_STANDBY;
  88. break;
  89. }
  90. return mode;
  91. }
  92. static int pv88060_buck_set_mode(struct regulator_dev *rdev,
  93. unsigned int mode)
  94. {
  95. struct pv88060_regulator *info = rdev_get_drvdata(rdev);
  96. int val = 0;
  97. switch (mode) {
  98. case REGULATOR_MODE_FAST:
  99. val = PV88060_BUCK_MODE_SYNC;
  100. break;
  101. case REGULATOR_MODE_NORMAL:
  102. val = PV88060_BUCK_MODE_AUTO;
  103. break;
  104. case REGULATOR_MODE_STANDBY:
  105. val = PV88060_BUCK_MODE_SLEEP;
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. return regmap_update_bits(rdev->regmap, info->conf,
  111. PV88060_BUCK_MODE_MASK, val);
  112. }
  113. static int pv88060_set_current_limit(struct regulator_dev *rdev, int min,
  114. int max)
  115. {
  116. struct pv88060_regulator *info = rdev_get_drvdata(rdev);
  117. int i;
  118. /* search for closest to maximum */
  119. for (i = info->n_current_limits - 1; i >= 0; i--) {
  120. if (min <= info->current_limits[i]
  121. && max >= info->current_limits[i]) {
  122. return regmap_update_bits(rdev->regmap,
  123. info->conf,
  124. info->limit_mask,
  125. i << PV88060_BUCK_ILIM_SHIFT);
  126. }
  127. }
  128. return -EINVAL;
  129. }
  130. static int pv88060_get_current_limit(struct regulator_dev *rdev)
  131. {
  132. struct pv88060_regulator *info = rdev_get_drvdata(rdev);
  133. unsigned int data;
  134. int ret;
  135. ret = regmap_read(rdev->regmap, info->conf, &data);
  136. if (ret < 0)
  137. return ret;
  138. data = (data & info->limit_mask) >> PV88060_BUCK_ILIM_SHIFT;
  139. return info->current_limits[data];
  140. }
  141. static const struct regulator_ops pv88060_buck_ops = {
  142. .get_mode = pv88060_buck_get_mode,
  143. .set_mode = pv88060_buck_set_mode,
  144. .enable = regulator_enable_regmap,
  145. .disable = regulator_disable_regmap,
  146. .is_enabled = regulator_is_enabled_regmap,
  147. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  148. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  149. .list_voltage = regulator_list_voltage_linear,
  150. .set_current_limit = pv88060_set_current_limit,
  151. .get_current_limit = pv88060_get_current_limit,
  152. };
  153. static const struct regulator_ops pv88060_ldo_ops = {
  154. .enable = regulator_enable_regmap,
  155. .disable = regulator_disable_regmap,
  156. .is_enabled = regulator_is_enabled_regmap,
  157. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  158. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  159. .list_voltage = regulator_list_voltage_linear,
  160. };
  161. #define PV88060_BUCK(chip, regl_name, min, step, max, limits_array) \
  162. {\
  163. .desc = {\
  164. .id = chip##_ID_##regl_name,\
  165. .name = __stringify(chip##_##regl_name),\
  166. .of_match = of_match_ptr(#regl_name),\
  167. .regulators_node = of_match_ptr("regulators"),\
  168. .type = REGULATOR_VOLTAGE,\
  169. .owner = THIS_MODULE,\
  170. .ops = &pv88060_buck_ops,\
  171. .min_uV = min,\
  172. .uV_step = step,\
  173. .n_voltages = ((max) - (min))/(step) + 1,\
  174. .enable_reg = PV88060_REG_##regl_name##_CONF0,\
  175. .enable_mask = PV88060_BUCK_EN, \
  176. .vsel_reg = PV88060_REG_##regl_name##_CONF0,\
  177. .vsel_mask = PV88060_VBUCK_MASK,\
  178. },\
  179. .current_limits = limits_array,\
  180. .n_current_limits = ARRAY_SIZE(limits_array),\
  181. .limit_mask = PV88060_BUCK_ILIM_MASK, \
  182. .conf = PV88060_REG_##regl_name##_CONF1,\
  183. }
  184. #define PV88060_LDO(chip, regl_name, min, step, max) \
  185. {\
  186. .desc = {\
  187. .id = chip##_ID_##regl_name,\
  188. .name = __stringify(chip##_##regl_name),\
  189. .of_match = of_match_ptr(#regl_name),\
  190. .regulators_node = of_match_ptr("regulators"),\
  191. .type = REGULATOR_VOLTAGE,\
  192. .owner = THIS_MODULE,\
  193. .ops = &pv88060_ldo_ops,\
  194. .min_uV = min, \
  195. .uV_step = step, \
  196. .n_voltages = (step) ? ((max - min) / step + 1) : 1, \
  197. .enable_reg = PV88060_REG_##regl_name##_CONF, \
  198. .enable_mask = PV88060_LDO_EN, \
  199. .vsel_reg = PV88060_REG_##regl_name##_CONF, \
  200. .vsel_mask = PV88060_VLDO_MASK, \
  201. },\
  202. }
  203. #define PV88060_SW(chip, regl_name, max) \
  204. {\
  205. .desc = {\
  206. .id = chip##_ID_##regl_name,\
  207. .name = __stringify(chip##_##regl_name),\
  208. .of_match = of_match_ptr(#regl_name),\
  209. .regulators_node = of_match_ptr("regulators"),\
  210. .type = REGULATOR_VOLTAGE,\
  211. .owner = THIS_MODULE,\
  212. .ops = &pv88060_ldo_ops,\
  213. .min_uV = max,\
  214. .uV_step = 0,\
  215. .n_voltages = 1,\
  216. .enable_reg = PV88060_REG_##regl_name##_CONF,\
  217. .enable_mask = PV88060_SW_EN,\
  218. },\
  219. }
  220. static const struct pv88060_regulator pv88060_regulator_info[] = {
  221. PV88060_BUCK(PV88060, BUCK1, 2800000, 12500, 4387500,
  222. pv88060_buck1_limits),
  223. PV88060_LDO(PV88060, LDO1, 1200000, 50000, 3350000),
  224. PV88060_LDO(PV88060, LDO2, 1200000, 50000, 3350000),
  225. PV88060_LDO(PV88060, LDO3, 1200000, 50000, 3350000),
  226. PV88060_LDO(PV88060, LDO4, 1200000, 50000, 3350000),
  227. PV88060_LDO(PV88060, LDO5, 1200000, 50000, 3350000),
  228. PV88060_LDO(PV88060, LDO6, 1200000, 50000, 3350000),
  229. PV88060_LDO(PV88060, LDO7, 1200000, 50000, 3350000),
  230. PV88060_SW(PV88060, SW1, 5000000),
  231. PV88060_SW(PV88060, SW2, 5000000),
  232. PV88060_SW(PV88060, SW3, 5000000),
  233. PV88060_SW(PV88060, SW4, 5000000),
  234. PV88060_SW(PV88060, SW5, 5000000),
  235. PV88060_SW(PV88060, SW6, 5000000),
  236. };
  237. static irqreturn_t pv88060_irq_handler(int irq, void *data)
  238. {
  239. struct pv88060 *chip = data;
  240. int i, reg_val, err, ret = IRQ_NONE;
  241. err = regmap_read(chip->regmap, PV88060_REG_EVENT_A, &reg_val);
  242. if (err < 0)
  243. goto error_i2c;
  244. if (reg_val & PV88060_E_VDD_FLT) {
  245. for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
  246. if (chip->rdev[i] != NULL) {
  247. regulator_notifier_call_chain(chip->rdev[i],
  248. REGULATOR_EVENT_UNDER_VOLTAGE,
  249. NULL);
  250. }
  251. }
  252. err = regmap_write(chip->regmap, PV88060_REG_EVENT_A,
  253. PV88060_E_VDD_FLT);
  254. if (err < 0)
  255. goto error_i2c;
  256. ret = IRQ_HANDLED;
  257. }
  258. if (reg_val & PV88060_E_OVER_TEMP) {
  259. for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
  260. if (chip->rdev[i] != NULL) {
  261. regulator_notifier_call_chain(chip->rdev[i],
  262. REGULATOR_EVENT_OVER_TEMP,
  263. NULL);
  264. }
  265. }
  266. err = regmap_write(chip->regmap, PV88060_REG_EVENT_A,
  267. PV88060_E_OVER_TEMP);
  268. if (err < 0)
  269. goto error_i2c;
  270. ret = IRQ_HANDLED;
  271. }
  272. return ret;
  273. error_i2c:
  274. dev_err(chip->dev, "I2C error : %d\n", err);
  275. return IRQ_NONE;
  276. }
  277. /*
  278. * I2C driver interface functions
  279. */
  280. static int pv88060_i2c_probe(struct i2c_client *i2c,
  281. const struct i2c_device_id *id)
  282. {
  283. struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
  284. struct pv88060 *chip;
  285. struct regulator_config config = { };
  286. int error, i, ret = 0;
  287. chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88060), GFP_KERNEL);
  288. if (!chip)
  289. return -ENOMEM;
  290. chip->dev = &i2c->dev;
  291. chip->regmap = devm_regmap_init_i2c(i2c, &pv88060_regmap_config);
  292. if (IS_ERR(chip->regmap)) {
  293. error = PTR_ERR(chip->regmap);
  294. dev_err(chip->dev, "Failed to allocate register map: %d\n",
  295. error);
  296. return error;
  297. }
  298. i2c_set_clientdata(i2c, chip);
  299. if (i2c->irq != 0) {
  300. ret = regmap_write(chip->regmap, PV88060_REG_MASK_A, 0xFF);
  301. if (ret < 0) {
  302. dev_err(chip->dev,
  303. "Failed to mask A reg: %d\n", ret);
  304. return ret;
  305. }
  306. ret = regmap_write(chip->regmap, PV88060_REG_MASK_B, 0xFF);
  307. if (ret < 0) {
  308. dev_err(chip->dev,
  309. "Failed to mask B reg: %d\n", ret);
  310. return ret;
  311. }
  312. ret = regmap_write(chip->regmap, PV88060_REG_MASK_C, 0xFF);
  313. if (ret < 0) {
  314. dev_err(chip->dev,
  315. "Failed to mask C reg: %d\n", ret);
  316. return ret;
  317. }
  318. ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
  319. pv88060_irq_handler,
  320. IRQF_TRIGGER_LOW|IRQF_ONESHOT,
  321. "pv88060", chip);
  322. if (ret != 0) {
  323. dev_err(chip->dev, "Failed to request IRQ: %d\n",
  324. i2c->irq);
  325. return ret;
  326. }
  327. ret = regmap_update_bits(chip->regmap, PV88060_REG_MASK_A,
  328. PV88060_M_VDD_FLT | PV88060_M_OVER_TEMP, 0);
  329. if (ret < 0) {
  330. dev_err(chip->dev,
  331. "Failed to update mask reg: %d\n", ret);
  332. return ret;
  333. }
  334. } else {
  335. dev_warn(chip->dev, "No IRQ configured\n");
  336. }
  337. config.dev = chip->dev;
  338. config.regmap = chip->regmap;
  339. for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
  340. if (init_data)
  341. config.init_data = &init_data[i];
  342. config.driver_data = (void *)&pv88060_regulator_info[i];
  343. chip->rdev[i] = devm_regulator_register(chip->dev,
  344. &pv88060_regulator_info[i].desc, &config);
  345. if (IS_ERR(chip->rdev[i])) {
  346. dev_err(chip->dev,
  347. "Failed to register PV88060 regulator\n");
  348. return PTR_ERR(chip->rdev[i]);
  349. }
  350. }
  351. return 0;
  352. }
  353. static const struct i2c_device_id pv88060_i2c_id[] = {
  354. {"pv88060", 0},
  355. {},
  356. };
  357. MODULE_DEVICE_TABLE(i2c, pv88060_i2c_id);
  358. #ifdef CONFIG_OF
  359. static const struct of_device_id pv88060_dt_ids[] = {
  360. { .compatible = "pvs,pv88060", .data = &pv88060_i2c_id[0] },
  361. {},
  362. };
  363. MODULE_DEVICE_TABLE(of, pv88060_dt_ids);
  364. #endif
  365. static struct i2c_driver pv88060_regulator_driver = {
  366. .driver = {
  367. .name = "pv88060",
  368. .of_match_table = of_match_ptr(pv88060_dt_ids),
  369. },
  370. .probe = pv88060_i2c_probe,
  371. .id_table = pv88060_i2c_id,
  372. };
  373. module_i2c_driver(pv88060_regulator_driver);
  374. MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
  375. MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88060");
  376. MODULE_LICENSE("GPL");