pv88090-regulator.c 11 KB

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