pv88090-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // pv88090-regulator.c - Regulator device driver for PV88090
  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 "pv88090-regulator.h"
  17. #define PV88090_MAX_REGULATORS 5
  18. /* PV88090 REGULATOR IDs */
  19. enum {
  20. /* BUCKs */
  21. PV88090_ID_BUCK1,
  22. PV88090_ID_BUCK2,
  23. PV88090_ID_BUCK3,
  24. /* LDOs */
  25. PV88090_ID_LDO1,
  26. PV88090_ID_LDO2,
  27. };
  28. struct pv88090_regulator {
  29. struct regulator_desc desc;
  30. unsigned int conf;
  31. unsigned int conf2;
  32. };
  33. struct pv88090 {
  34. struct device *dev;
  35. struct regmap *regmap;
  36. struct regulator_dev *rdev[PV88090_MAX_REGULATORS];
  37. };
  38. struct pv88090_buck_voltage {
  39. int min_uV;
  40. int max_uV;
  41. int uV_step;
  42. };
  43. static const struct regmap_config pv88090_regmap_config = {
  44. .reg_bits = 8,
  45. .val_bits = 8,
  46. };
  47. /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
  48. * Entry indexes corresponds to register values.
  49. */
  50. static const unsigned int pv88090_buck1_limits[] = {
  51. 220000, 440000, 660000, 880000, 1100000, 1320000, 1540000, 1760000,
  52. 1980000, 2200000, 2420000, 2640000, 2860000, 3080000, 3300000, 3520000,
  53. 3740000, 3960000, 4180000, 4400000, 4620000, 4840000, 5060000, 5280000,
  54. 5500000, 5720000, 5940000, 6160000, 6380000, 6600000, 6820000, 7040000
  55. };
  56. static const unsigned int pv88090_buck23_limits[] = {
  57. 1496000, 2393000, 3291000, 4189000
  58. };
  59. static const struct pv88090_buck_voltage pv88090_buck_vol[3] = {
  60. {
  61. .min_uV = 600000,
  62. .max_uV = 1393750,
  63. .uV_step = 6250,
  64. },
  65. {
  66. .min_uV = 1400000,
  67. .max_uV = 2193750,
  68. .uV_step = 6250,
  69. },
  70. {
  71. .min_uV = 1250000,
  72. .max_uV = 2837500,
  73. .uV_step = 12500,
  74. },
  75. };
  76. static unsigned int pv88090_buck_get_mode(struct regulator_dev *rdev)
  77. {
  78. struct pv88090_regulator *info = rdev_get_drvdata(rdev);
  79. unsigned int data;
  80. int ret, mode = 0;
  81. ret = regmap_read(rdev->regmap, info->conf, &data);
  82. if (ret < 0)
  83. return ret;
  84. switch (data & PV88090_BUCK1_MODE_MASK) {
  85. case PV88090_BUCK_MODE_SYNC:
  86. mode = REGULATOR_MODE_FAST;
  87. break;
  88. case PV88090_BUCK_MODE_AUTO:
  89. mode = REGULATOR_MODE_NORMAL;
  90. break;
  91. case PV88090_BUCK_MODE_SLEEP:
  92. mode = REGULATOR_MODE_STANDBY;
  93. break;
  94. }
  95. return mode;
  96. }
  97. static int pv88090_buck_set_mode(struct regulator_dev *rdev,
  98. unsigned int mode)
  99. {
  100. struct pv88090_regulator *info = rdev_get_drvdata(rdev);
  101. int val = 0;
  102. switch (mode) {
  103. case REGULATOR_MODE_FAST:
  104. val = PV88090_BUCK_MODE_SYNC;
  105. break;
  106. case REGULATOR_MODE_NORMAL:
  107. val = PV88090_BUCK_MODE_AUTO;
  108. break;
  109. case REGULATOR_MODE_STANDBY:
  110. val = PV88090_BUCK_MODE_SLEEP;
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. return regmap_update_bits(rdev->regmap, info->conf,
  116. PV88090_BUCK1_MODE_MASK, val);
  117. }
  118. static const struct regulator_ops pv88090_buck_ops = {
  119. .get_mode = pv88090_buck_get_mode,
  120. .set_mode = pv88090_buck_set_mode,
  121. .enable = regulator_enable_regmap,
  122. .disable = regulator_disable_regmap,
  123. .is_enabled = regulator_is_enabled_regmap,
  124. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  125. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  126. .list_voltage = regulator_list_voltage_linear,
  127. .set_current_limit = regulator_set_current_limit_regmap,
  128. .get_current_limit = regulator_get_current_limit_regmap,
  129. };
  130. static const struct regulator_ops pv88090_ldo_ops = {
  131. .enable = regulator_enable_regmap,
  132. .disable = regulator_disable_regmap,
  133. .is_enabled = regulator_is_enabled_regmap,
  134. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  135. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  136. .list_voltage = regulator_list_voltage_linear,
  137. };
  138. #define PV88090_BUCK(chip, regl_name, min, step, max, limits_array) \
  139. {\
  140. .desc = {\
  141. .id = chip##_ID_##regl_name,\
  142. .name = __stringify(chip##_##regl_name),\
  143. .of_match = of_match_ptr(#regl_name),\
  144. .regulators_node = of_match_ptr("regulators"),\
  145. .type = REGULATOR_VOLTAGE,\
  146. .owner = THIS_MODULE,\
  147. .ops = &pv88090_buck_ops,\
  148. .min_uV = min, \
  149. .uV_step = step, \
  150. .n_voltages = ((max) - (min))/(step) + 1, \
  151. .enable_reg = PV88090_REG_##regl_name##_CONF0, \
  152. .enable_mask = PV88090_##regl_name##_EN, \
  153. .vsel_reg = PV88090_REG_##regl_name##_CONF0, \
  154. .vsel_mask = PV88090_V##regl_name##_MASK, \
  155. .curr_table = limits_array, \
  156. .n_current_limits = ARRAY_SIZE(limits_array), \
  157. .csel_reg = PV88090_REG_##regl_name##_CONF1, \
  158. .csel_mask = PV88090_##regl_name##_ILIM_MASK, \
  159. },\
  160. .conf = PV88090_REG_##regl_name##_CONF1, \
  161. .conf2 = PV88090_REG_##regl_name##_CONF2, \
  162. }
  163. #define PV88090_LDO(chip, regl_name, min, step, max) \
  164. {\
  165. .desc = {\
  166. .id = chip##_ID_##regl_name,\
  167. .name = __stringify(chip##_##regl_name),\
  168. .of_match = of_match_ptr(#regl_name),\
  169. .regulators_node = of_match_ptr("regulators"),\
  170. .type = REGULATOR_VOLTAGE,\
  171. .owner = THIS_MODULE,\
  172. .ops = &pv88090_ldo_ops,\
  173. .min_uV = min, \
  174. .uV_step = step, \
  175. .n_voltages = ((max) - (min))/(step) + 1, \
  176. .enable_reg = PV88090_REG_##regl_name##_CONT, \
  177. .enable_mask = PV88090_##regl_name##_EN, \
  178. .vsel_reg = PV88090_REG_##regl_name##_CONT, \
  179. .vsel_mask = PV88090_V##regl_name##_MASK, \
  180. },\
  181. }
  182. static struct pv88090_regulator pv88090_regulator_info[] = {
  183. PV88090_BUCK(PV88090, BUCK1, 600000, 6250, 1393750,
  184. pv88090_buck1_limits),
  185. PV88090_BUCK(PV88090, BUCK2, 600000, 6250, 1393750,
  186. pv88090_buck23_limits),
  187. PV88090_BUCK(PV88090, BUCK3, 600000, 6250, 1393750,
  188. pv88090_buck23_limits),
  189. PV88090_LDO(PV88090, LDO1, 1200000, 50000, 4350000),
  190. PV88090_LDO(PV88090, LDO2, 650000, 25000, 2225000),
  191. };
  192. static irqreturn_t pv88090_irq_handler(int irq, void *data)
  193. {
  194. struct pv88090 *chip = data;
  195. int i, reg_val, err, ret = IRQ_NONE;
  196. err = regmap_read(chip->regmap, PV88090_REG_EVENT_A, &reg_val);
  197. if (err < 0)
  198. goto error_i2c;
  199. if (reg_val & PV88090_E_VDD_FLT) {
  200. for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
  201. if (chip->rdev[i] != NULL) {
  202. regulator_lock(chip->rdev[i]);
  203. regulator_notifier_call_chain(chip->rdev[i],
  204. REGULATOR_EVENT_UNDER_VOLTAGE,
  205. NULL);
  206. regulator_unlock(chip->rdev[i]);
  207. }
  208. }
  209. err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
  210. PV88090_E_VDD_FLT);
  211. if (err < 0)
  212. goto error_i2c;
  213. ret = IRQ_HANDLED;
  214. }
  215. if (reg_val & PV88090_E_OVER_TEMP) {
  216. for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
  217. if (chip->rdev[i] != NULL) {
  218. regulator_lock(chip->rdev[i]);
  219. regulator_notifier_call_chain(chip->rdev[i],
  220. REGULATOR_EVENT_OVER_TEMP,
  221. NULL);
  222. regulator_unlock(chip->rdev[i]);
  223. }
  224. }
  225. err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
  226. PV88090_E_OVER_TEMP);
  227. if (err < 0)
  228. goto error_i2c;
  229. ret = IRQ_HANDLED;
  230. }
  231. return ret;
  232. error_i2c:
  233. dev_err(chip->dev, "I2C error : %d\n", err);
  234. return IRQ_NONE;
  235. }
  236. /*
  237. * I2C driver interface functions
  238. */
  239. static int pv88090_i2c_probe(struct i2c_client *i2c,
  240. const struct i2c_device_id *id)
  241. {
  242. struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
  243. struct pv88090 *chip;
  244. struct regulator_config config = { };
  245. int error, i, ret = 0;
  246. unsigned int conf2, range, index;
  247. chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88090), GFP_KERNEL);
  248. if (!chip)
  249. return -ENOMEM;
  250. chip->dev = &i2c->dev;
  251. chip->regmap = devm_regmap_init_i2c(i2c, &pv88090_regmap_config);
  252. if (IS_ERR(chip->regmap)) {
  253. error = PTR_ERR(chip->regmap);
  254. dev_err(chip->dev, "Failed to allocate register map: %d\n",
  255. error);
  256. return error;
  257. }
  258. i2c_set_clientdata(i2c, chip);
  259. if (i2c->irq != 0) {
  260. ret = regmap_write(chip->regmap, PV88090_REG_MASK_A, 0xFF);
  261. if (ret < 0) {
  262. dev_err(chip->dev,
  263. "Failed to mask A reg: %d\n", ret);
  264. return ret;
  265. }
  266. ret = regmap_write(chip->regmap, PV88090_REG_MASK_B, 0xFF);
  267. if (ret < 0) {
  268. dev_err(chip->dev,
  269. "Failed to mask B reg: %d\n", ret);
  270. return ret;
  271. }
  272. ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
  273. pv88090_irq_handler,
  274. IRQF_TRIGGER_LOW|IRQF_ONESHOT,
  275. "pv88090", chip);
  276. if (ret != 0) {
  277. dev_err(chip->dev, "Failed to request IRQ: %d\n",
  278. i2c->irq);
  279. return ret;
  280. }
  281. ret = regmap_update_bits(chip->regmap, PV88090_REG_MASK_A,
  282. PV88090_M_VDD_FLT | PV88090_M_OVER_TEMP, 0);
  283. if (ret < 0) {
  284. dev_err(chip->dev,
  285. "Failed to update mask reg: %d\n", ret);
  286. return ret;
  287. }
  288. } else {
  289. dev_warn(chip->dev, "No IRQ configured\n");
  290. }
  291. config.dev = chip->dev;
  292. config.regmap = chip->regmap;
  293. for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
  294. if (init_data)
  295. config.init_data = &init_data[i];
  296. if (i == PV88090_ID_BUCK2 || i == PV88090_ID_BUCK3) {
  297. ret = regmap_read(chip->regmap,
  298. pv88090_regulator_info[i].conf2, &conf2);
  299. if (ret < 0)
  300. return ret;
  301. conf2 = (conf2 >> PV88090_BUCK_VDAC_RANGE_SHIFT) &
  302. PV88090_BUCK_VDAC_RANGE_MASK;
  303. ret = regmap_read(chip->regmap,
  304. PV88090_REG_BUCK_FOLD_RANGE, &range);
  305. if (ret < 0)
  306. return ret;
  307. range = (range >>
  308. (PV88090_BUCK_VRANGE_GAIN_SHIFT + i - 1)) &
  309. PV88090_BUCK_VRANGE_GAIN_MASK;
  310. index = ((range << 1) | conf2);
  311. if (index > PV88090_ID_BUCK3) {
  312. dev_err(chip->dev,
  313. "Invalid index(%d)\n", index);
  314. return -EINVAL;
  315. }
  316. pv88090_regulator_info[i].desc.min_uV
  317. = pv88090_buck_vol[index].min_uV;
  318. pv88090_regulator_info[i].desc.uV_step
  319. = pv88090_buck_vol[index].uV_step;
  320. pv88090_regulator_info[i].desc.n_voltages
  321. = ((pv88090_buck_vol[index].max_uV)
  322. - (pv88090_buck_vol[index].min_uV))
  323. /(pv88090_buck_vol[index].uV_step) + 1;
  324. }
  325. config.driver_data = (void *)&pv88090_regulator_info[i];
  326. chip->rdev[i] = devm_regulator_register(chip->dev,
  327. &pv88090_regulator_info[i].desc, &config);
  328. if (IS_ERR(chip->rdev[i])) {
  329. dev_err(chip->dev,
  330. "Failed to register PV88090 regulator\n");
  331. return PTR_ERR(chip->rdev[i]);
  332. }
  333. }
  334. return 0;
  335. }
  336. static const struct i2c_device_id pv88090_i2c_id[] = {
  337. {"pv88090", 0},
  338. {},
  339. };
  340. MODULE_DEVICE_TABLE(i2c, pv88090_i2c_id);
  341. #ifdef CONFIG_OF
  342. static const struct of_device_id pv88090_dt_ids[] = {
  343. { .compatible = "pvs,pv88090", .data = &pv88090_i2c_id[0] },
  344. {},
  345. };
  346. MODULE_DEVICE_TABLE(of, pv88090_dt_ids);
  347. #endif
  348. static struct i2c_driver pv88090_regulator_driver = {
  349. .driver = {
  350. .name = "pv88090",
  351. .of_match_table = of_match_ptr(pv88090_dt_ids),
  352. },
  353. .probe = pv88090_i2c_probe,
  354. .id_table = pv88090_i2c_id,
  355. };
  356. module_i2c_driver(pv88090_regulator_driver);
  357. MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
  358. MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88090");
  359. MODULE_LICENSE("GPL");