pv88090-regulator.c 11 KB

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