pv88080-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * pv88080-regulator.c - Regulator device driver for PV88080
  3. * Copyright (C) 2016 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 "pv88080-regulator.h"
  27. #define PV88080_MAX_REGULATORS 3
  28. /* PV88080 REGULATOR IDs */
  29. enum {
  30. /* BUCKs */
  31. PV88080_ID_BUCK1,
  32. PV88080_ID_BUCK2,
  33. PV88080_ID_BUCK3,
  34. };
  35. struct pv88080_regulator {
  36. struct regulator_desc desc;
  37. /* Current limiting */
  38. unsigned int n_current_limits;
  39. const int *current_limits;
  40. unsigned int limit_mask;
  41. unsigned int conf;
  42. unsigned int conf2;
  43. unsigned int conf5;
  44. };
  45. struct pv88080 {
  46. struct device *dev;
  47. struct regmap *regmap;
  48. struct regulator_dev *rdev[PV88080_MAX_REGULATORS];
  49. };
  50. struct pv88080_buck_voltage {
  51. int min_uV;
  52. int max_uV;
  53. int uV_step;
  54. };
  55. static const struct regmap_config pv88080_regmap_config = {
  56. .reg_bits = 8,
  57. .val_bits = 8,
  58. };
  59. /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
  60. * Entry indexes corresponds to register values.
  61. */
  62. static const int pv88080_buck1_limits[] = {
  63. 3230000, 5130000, 6960000, 8790000
  64. };
  65. static const int pv88080_buck23_limits[] = {
  66. 1496000, 2393000, 3291000, 4189000
  67. };
  68. static const struct pv88080_buck_voltage pv88080_buck_vol[2] = {
  69. {
  70. .min_uV = 600000,
  71. .max_uV = 1393750,
  72. .uV_step = 6250,
  73. },
  74. {
  75. .min_uV = 1400000,
  76. .max_uV = 2193750,
  77. .uV_step = 6250,
  78. },
  79. };
  80. static unsigned int pv88080_buck_get_mode(struct regulator_dev *rdev)
  81. {
  82. struct pv88080_regulator *info = rdev_get_drvdata(rdev);
  83. unsigned int data;
  84. int ret, mode = 0;
  85. ret = regmap_read(rdev->regmap, info->conf, &data);
  86. if (ret < 0)
  87. return ret;
  88. switch (data & PV88080_BUCK1_MODE_MASK) {
  89. case PV88080_BUCK_MODE_SYNC:
  90. mode = REGULATOR_MODE_FAST;
  91. break;
  92. case PV88080_BUCK_MODE_AUTO:
  93. mode = REGULATOR_MODE_NORMAL;
  94. break;
  95. case PV88080_BUCK_MODE_SLEEP:
  96. mode = REGULATOR_MODE_STANDBY;
  97. break;
  98. default:
  99. return -EINVAL;
  100. }
  101. return mode;
  102. }
  103. static int pv88080_buck_set_mode(struct regulator_dev *rdev,
  104. unsigned int mode)
  105. {
  106. struct pv88080_regulator *info = rdev_get_drvdata(rdev);
  107. int val = 0;
  108. switch (mode) {
  109. case REGULATOR_MODE_FAST:
  110. val = PV88080_BUCK_MODE_SYNC;
  111. break;
  112. case REGULATOR_MODE_NORMAL:
  113. val = PV88080_BUCK_MODE_AUTO;
  114. break;
  115. case REGULATOR_MODE_STANDBY:
  116. val = PV88080_BUCK_MODE_SLEEP;
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. return regmap_update_bits(rdev->regmap, info->conf,
  122. PV88080_BUCK1_MODE_MASK, val);
  123. }
  124. static int pv88080_set_current_limit(struct regulator_dev *rdev, int min,
  125. int max)
  126. {
  127. struct pv88080_regulator *info = rdev_get_drvdata(rdev);
  128. int i;
  129. /* search for closest to maximum */
  130. for (i = info->n_current_limits; i >= 0; i--) {
  131. if (min <= info->current_limits[i]
  132. && max >= info->current_limits[i]) {
  133. return regmap_update_bits(rdev->regmap,
  134. info->conf,
  135. info->limit_mask,
  136. i << PV88080_BUCK1_ILIM_SHIFT);
  137. }
  138. }
  139. return -EINVAL;
  140. }
  141. static int pv88080_get_current_limit(struct regulator_dev *rdev)
  142. {
  143. struct pv88080_regulator *info = rdev_get_drvdata(rdev);
  144. unsigned int data;
  145. int ret;
  146. ret = regmap_read(rdev->regmap, info->conf, &data);
  147. if (ret < 0)
  148. return ret;
  149. data = (data & info->limit_mask) >> PV88080_BUCK1_ILIM_SHIFT;
  150. return info->current_limits[data];
  151. }
  152. static struct regulator_ops pv88080_buck_ops = {
  153. .get_mode = pv88080_buck_get_mode,
  154. .set_mode = pv88080_buck_set_mode,
  155. .enable = regulator_enable_regmap,
  156. .disable = regulator_disable_regmap,
  157. .is_enabled = regulator_is_enabled_regmap,
  158. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  159. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  160. .list_voltage = regulator_list_voltage_linear,
  161. .set_current_limit = pv88080_set_current_limit,
  162. .get_current_limit = pv88080_get_current_limit,
  163. };
  164. #define PV88080_BUCK(chip, regl_name, min, step, max, limits_array) \
  165. {\
  166. .desc = {\
  167. .id = chip##_ID_##regl_name,\
  168. .name = __stringify(chip##_##regl_name),\
  169. .of_match = of_match_ptr(#regl_name),\
  170. .regulators_node = of_match_ptr("regulators"),\
  171. .type = REGULATOR_VOLTAGE,\
  172. .owner = THIS_MODULE,\
  173. .ops = &pv88080_buck_ops,\
  174. .min_uV = min, \
  175. .uV_step = step, \
  176. .n_voltages = ((max) - (min))/(step) + 1, \
  177. .enable_reg = PV88080_REG_##regl_name##_CONF0, \
  178. .enable_mask = PV88080_##regl_name##_EN, \
  179. .vsel_reg = PV88080_REG_##regl_name##_CONF0, \
  180. .vsel_mask = PV88080_V##regl_name##_MASK, \
  181. },\
  182. .current_limits = limits_array, \
  183. .n_current_limits = ARRAY_SIZE(limits_array), \
  184. .limit_mask = PV88080_##regl_name##_ILIM_MASK, \
  185. .conf = PV88080_REG_##regl_name##_CONF1, \
  186. .conf2 = PV88080_REG_##regl_name##_CONF2, \
  187. .conf5 = PV88080_REG_##regl_name##_CONF5, \
  188. }
  189. static struct pv88080_regulator pv88080_regulator_info[] = {
  190. PV88080_BUCK(PV88080, BUCK1, 600000, 6250, 1393750,
  191. pv88080_buck1_limits),
  192. PV88080_BUCK(PV88080, BUCK2, 600000, 6250, 1393750,
  193. pv88080_buck23_limits),
  194. PV88080_BUCK(PV88080, BUCK3, 600000, 6250, 1393750,
  195. pv88080_buck23_limits),
  196. };
  197. static irqreturn_t pv88080_irq_handler(int irq, void *data)
  198. {
  199. struct pv88080 *chip = data;
  200. int i, reg_val, err, ret = IRQ_NONE;
  201. err = regmap_read(chip->regmap, PV88080_REG_EVENT_A, &reg_val);
  202. if (err < 0)
  203. goto error_i2c;
  204. if (reg_val & PV88080_E_VDD_FLT) {
  205. for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
  206. if (chip->rdev[i] != NULL) {
  207. regulator_notifier_call_chain(chip->rdev[i],
  208. REGULATOR_EVENT_UNDER_VOLTAGE,
  209. NULL);
  210. }
  211. }
  212. err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
  213. PV88080_E_VDD_FLT);
  214. if (err < 0)
  215. goto error_i2c;
  216. ret = IRQ_HANDLED;
  217. }
  218. if (reg_val & PV88080_E_OVER_TEMP) {
  219. for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
  220. if (chip->rdev[i] != NULL) {
  221. regulator_notifier_call_chain(chip->rdev[i],
  222. REGULATOR_EVENT_OVER_TEMP,
  223. NULL);
  224. }
  225. }
  226. err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
  227. PV88080_E_OVER_TEMP);
  228. if (err < 0)
  229. goto error_i2c;
  230. ret = IRQ_HANDLED;
  231. }
  232. return ret;
  233. error_i2c:
  234. dev_err(chip->dev, "I2C error : %d\n", err);
  235. return IRQ_NONE;
  236. }
  237. /*
  238. * I2C driver interface functions
  239. */
  240. static int pv88080_i2c_probe(struct i2c_client *i2c,
  241. const struct i2c_device_id *id)
  242. {
  243. struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
  244. struct pv88080 *chip;
  245. struct regulator_config config = { };
  246. int i, error, ret;
  247. unsigned int conf2, conf5;
  248. chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88080), GFP_KERNEL);
  249. if (!chip)
  250. return -ENOMEM;
  251. chip->dev = &i2c->dev;
  252. chip->regmap = devm_regmap_init_i2c(i2c, &pv88080_regmap_config);
  253. if (IS_ERR(chip->regmap)) {
  254. error = PTR_ERR(chip->regmap);
  255. dev_err(chip->dev, "Failed to allocate register map: %d\n",
  256. error);
  257. return error;
  258. }
  259. i2c_set_clientdata(i2c, chip);
  260. if (i2c->irq != 0) {
  261. ret = regmap_write(chip->regmap, PV88080_REG_MASK_A, 0xFF);
  262. if (ret < 0) {
  263. dev_err(chip->dev,
  264. "Failed to mask A reg: %d\n", ret);
  265. return ret;
  266. }
  267. ret = regmap_write(chip->regmap, PV88080_REG_MASK_B, 0xFF);
  268. if (ret < 0) {
  269. dev_err(chip->dev,
  270. "Failed to mask B reg: %d\n", ret);
  271. return ret;
  272. }
  273. ret = regmap_write(chip->regmap, PV88080_REG_MASK_C, 0xFF);
  274. if (ret < 0) {
  275. dev_err(chip->dev,
  276. "Failed to mask C reg: %d\n", ret);
  277. return ret;
  278. }
  279. ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
  280. pv88080_irq_handler,
  281. IRQF_TRIGGER_LOW|IRQF_ONESHOT,
  282. "pv88080", chip);
  283. if (ret != 0) {
  284. dev_err(chip->dev, "Failed to request IRQ: %d\n",
  285. i2c->irq);
  286. return ret;
  287. }
  288. ret = regmap_update_bits(chip->regmap, PV88080_REG_MASK_A,
  289. PV88080_M_VDD_FLT | PV88080_M_OVER_TEMP, 0);
  290. if (ret < 0) {
  291. dev_err(chip->dev,
  292. "Failed to update mask reg: %d\n", ret);
  293. return ret;
  294. }
  295. } else {
  296. dev_warn(chip->dev, "No IRQ configured\n");
  297. }
  298. config.dev = chip->dev;
  299. config.regmap = chip->regmap;
  300. for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
  301. if (init_data)
  302. config.init_data = &init_data[i];
  303. ret = regmap_read(chip->regmap,
  304. pv88080_regulator_info[i].conf2, &conf2);
  305. if (ret < 0)
  306. return ret;
  307. conf2 = ((conf2 >> PV88080_BUCK_VDAC_RANGE_SHIFT) &
  308. PV88080_BUCK_VDAC_RANGE_MASK);
  309. ret = regmap_read(chip->regmap,
  310. pv88080_regulator_info[i].conf5, &conf5);
  311. if (ret < 0)
  312. return ret;
  313. conf5 = ((conf5 >> PV88080_BUCK_VRANGE_GAIN_SHIFT) &
  314. PV88080_BUCK_VRANGE_GAIN_MASK);
  315. pv88080_regulator_info[i].desc.min_uV =
  316. pv88080_buck_vol[conf2].min_uV * (conf5+1);
  317. pv88080_regulator_info[i].desc.uV_step =
  318. pv88080_buck_vol[conf2].uV_step * (conf5+1);
  319. pv88080_regulator_info[i].desc.n_voltages =
  320. ((pv88080_buck_vol[conf2].max_uV * (conf5+1))
  321. - (pv88080_regulator_info[i].desc.min_uV))
  322. /(pv88080_regulator_info[i].desc.uV_step) + 1;
  323. config.driver_data = (void *)&pv88080_regulator_info[i];
  324. chip->rdev[i] = devm_regulator_register(chip->dev,
  325. &pv88080_regulator_info[i].desc, &config);
  326. if (IS_ERR(chip->rdev[i])) {
  327. dev_err(chip->dev,
  328. "Failed to register PV88080 regulator\n");
  329. return PTR_ERR(chip->rdev[i]);
  330. }
  331. }
  332. return 0;
  333. }
  334. static const struct i2c_device_id pv88080_i2c_id[] = {
  335. {"pv88080", 0},
  336. {},
  337. };
  338. MODULE_DEVICE_TABLE(i2c, pv88080_i2c_id);
  339. #ifdef CONFIG_OF
  340. static const struct of_device_id pv88080_dt_ids[] = {
  341. { .compatible = "pvs,pv88080", .data = &pv88080_i2c_id[0] },
  342. {},
  343. };
  344. MODULE_DEVICE_TABLE(of, pv88080_dt_ids);
  345. #endif
  346. static struct i2c_driver pv88080_regulator_driver = {
  347. .driver = {
  348. .name = "pv88080",
  349. .of_match_table = of_match_ptr(pv88080_dt_ids),
  350. },
  351. .probe = pv88080_i2c_probe,
  352. .id_table = pv88080_i2c_id,
  353. };
  354. module_i2c_driver(pv88080_regulator_driver);
  355. MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
  356. MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88080");
  357. MODULE_LICENSE("GPL");