pv88060-regulator.c 9.9 KB

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