da9211-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * da9211-regulator.c - Regulator device driver for DA9211/DA9212
  3. * /DA9213/DA9223/DA9214/DA9224/DA9215/DA9225
  4. * Copyright (C) 2015 Dialog Semiconductor Ltd.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. */
  16. #include <linux/err.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/gpio/consumer.h>
  27. #include <linux/regulator/of_regulator.h>
  28. #include <linux/regulator/da9211.h>
  29. #include "da9211-regulator.h"
  30. /* DEVICE IDs */
  31. #define DA9211_DEVICE_ID 0x22
  32. #define DA9213_DEVICE_ID 0x23
  33. #define DA9215_DEVICE_ID 0x24
  34. #define DA9211_BUCK_MODE_SLEEP 1
  35. #define DA9211_BUCK_MODE_SYNC 2
  36. #define DA9211_BUCK_MODE_AUTO 3
  37. /* DA9211 REGULATOR IDs */
  38. #define DA9211_ID_BUCKA 0
  39. #define DA9211_ID_BUCKB 1
  40. struct da9211 {
  41. struct device *dev;
  42. struct regmap *regmap;
  43. struct da9211_pdata *pdata;
  44. struct regulator_dev *rdev[DA9211_MAX_REGULATORS];
  45. int num_regulator;
  46. int chip_irq;
  47. int chip_id;
  48. };
  49. static const struct regmap_range_cfg da9211_regmap_range[] = {
  50. {
  51. .selector_reg = DA9211_REG_PAGE_CON,
  52. .selector_mask = DA9211_REG_PAGE_MASK,
  53. .selector_shift = DA9211_REG_PAGE_SHIFT,
  54. .window_start = 0,
  55. .window_len = 256,
  56. .range_min = 0,
  57. .range_max = 5*128,
  58. },
  59. };
  60. static const struct regmap_config da9211_regmap_config = {
  61. .reg_bits = 8,
  62. .val_bits = 8,
  63. .max_register = 5 * 128,
  64. .ranges = da9211_regmap_range,
  65. .num_ranges = ARRAY_SIZE(da9211_regmap_range),
  66. };
  67. /* Default limits measured in millivolts and milliamps */
  68. #define DA9211_MIN_MV 300
  69. #define DA9211_MAX_MV 1570
  70. #define DA9211_STEP_MV 10
  71. /* Current limits for DA9211 buck (uA) indices
  72. * corresponds with register values
  73. */
  74. static const int da9211_current_limits[] = {
  75. 2000000, 2200000, 2400000, 2600000, 2800000, 3000000, 3200000, 3400000,
  76. 3600000, 3800000, 4000000, 4200000, 4400000, 4600000, 4800000, 5000000
  77. };
  78. /* Current limits for DA9213 buck (uA) indices
  79. * corresponds with register values
  80. */
  81. static const int da9213_current_limits[] = {
  82. 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
  83. 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
  84. };
  85. /* Current limits for DA9215 buck (uA) indices
  86. * corresponds with register values
  87. */
  88. static const int da9215_current_limits[] = {
  89. 4000000, 4200000, 4400000, 4600000, 4800000, 5000000, 5200000, 5400000,
  90. 5600000, 5800000, 6000000, 6200000, 6400000, 6600000, 6800000, 7000000
  91. };
  92. static unsigned int da9211_buck_get_mode(struct regulator_dev *rdev)
  93. {
  94. int id = rdev_get_id(rdev);
  95. struct da9211 *chip = rdev_get_drvdata(rdev);
  96. unsigned int data;
  97. int ret, mode = 0;
  98. ret = regmap_read(chip->regmap, DA9211_REG_BUCKA_CONF+id, &data);
  99. if (ret < 0)
  100. return ret;
  101. switch (data & 0x03) {
  102. case DA9211_BUCK_MODE_SYNC:
  103. mode = REGULATOR_MODE_FAST;
  104. break;
  105. case DA9211_BUCK_MODE_AUTO:
  106. mode = REGULATOR_MODE_NORMAL;
  107. break;
  108. case DA9211_BUCK_MODE_SLEEP:
  109. mode = REGULATOR_MODE_STANDBY;
  110. break;
  111. }
  112. return mode;
  113. }
  114. static int da9211_buck_set_mode(struct regulator_dev *rdev,
  115. unsigned int mode)
  116. {
  117. int id = rdev_get_id(rdev);
  118. struct da9211 *chip = rdev_get_drvdata(rdev);
  119. int val = 0;
  120. switch (mode) {
  121. case REGULATOR_MODE_FAST:
  122. val = DA9211_BUCK_MODE_SYNC;
  123. break;
  124. case REGULATOR_MODE_NORMAL:
  125. val = DA9211_BUCK_MODE_AUTO;
  126. break;
  127. case REGULATOR_MODE_STANDBY:
  128. val = DA9211_BUCK_MODE_SLEEP;
  129. break;
  130. }
  131. return regmap_update_bits(chip->regmap, DA9211_REG_BUCKA_CONF+id,
  132. 0x03, val);
  133. }
  134. static int da9211_set_current_limit(struct regulator_dev *rdev, int min,
  135. int max)
  136. {
  137. int id = rdev_get_id(rdev);
  138. struct da9211 *chip = rdev_get_drvdata(rdev);
  139. int i, max_size;
  140. const int *current_limits;
  141. switch (chip->chip_id) {
  142. case DA9211:
  143. current_limits = da9211_current_limits;
  144. max_size = ARRAY_SIZE(da9211_current_limits)-1;
  145. break;
  146. case DA9213:
  147. current_limits = da9213_current_limits;
  148. max_size = ARRAY_SIZE(da9213_current_limits)-1;
  149. break;
  150. case DA9215:
  151. current_limits = da9215_current_limits;
  152. max_size = ARRAY_SIZE(da9215_current_limits)-1;
  153. break;
  154. default:
  155. return -EINVAL;
  156. }
  157. /* search for closest to maximum */
  158. for (i = max_size; i >= 0; i--) {
  159. if (min <= current_limits[i] &&
  160. max >= current_limits[i]) {
  161. return regmap_update_bits(chip->regmap,
  162. DA9211_REG_BUCK_ILIM,
  163. (0x0F << id*4), (i << id*4));
  164. }
  165. }
  166. return -EINVAL;
  167. }
  168. static int da9211_get_current_limit(struct regulator_dev *rdev)
  169. {
  170. int id = rdev_get_id(rdev);
  171. struct da9211 *chip = rdev_get_drvdata(rdev);
  172. unsigned int data;
  173. int ret;
  174. const int *current_limits;
  175. switch (chip->chip_id) {
  176. case DA9211:
  177. current_limits = da9211_current_limits;
  178. break;
  179. case DA9213:
  180. current_limits = da9213_current_limits;
  181. break;
  182. case DA9215:
  183. current_limits = da9215_current_limits;
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. ret = regmap_read(chip->regmap, DA9211_REG_BUCK_ILIM, &data);
  189. if (ret < 0)
  190. return ret;
  191. /* select one of 16 values: 0000 (2000mA or 3000mA)
  192. * to 1111 (5000mA or 6000mA).
  193. */
  194. data = (data >> id*4) & 0x0F;
  195. return current_limits[data];
  196. }
  197. static const struct regulator_ops da9211_buck_ops = {
  198. .get_mode = da9211_buck_get_mode,
  199. .set_mode = da9211_buck_set_mode,
  200. .enable = regulator_enable_regmap,
  201. .disable = regulator_disable_regmap,
  202. .is_enabled = regulator_is_enabled_regmap,
  203. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  204. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  205. .list_voltage = regulator_list_voltage_linear,
  206. .set_current_limit = da9211_set_current_limit,
  207. .get_current_limit = da9211_get_current_limit,
  208. };
  209. #define DA9211_BUCK(_id) \
  210. {\
  211. .name = #_id,\
  212. .ops = &da9211_buck_ops,\
  213. .type = REGULATOR_VOLTAGE,\
  214. .id = DA9211_ID_##_id,\
  215. .n_voltages = (DA9211_MAX_MV - DA9211_MIN_MV) / DA9211_STEP_MV + 1,\
  216. .min_uV = (DA9211_MIN_MV * 1000),\
  217. .uV_step = (DA9211_STEP_MV * 1000),\
  218. .enable_reg = DA9211_REG_BUCKA_CONT + DA9211_ID_##_id,\
  219. .enable_mask = DA9211_BUCKA_EN,\
  220. .vsel_reg = DA9211_REG_VBUCKA_A + DA9211_ID_##_id * 2,\
  221. .vsel_mask = DA9211_VBUCK_MASK,\
  222. .owner = THIS_MODULE,\
  223. }
  224. static struct regulator_desc da9211_regulators[] = {
  225. DA9211_BUCK(BUCKA),
  226. DA9211_BUCK(BUCKB),
  227. };
  228. #ifdef CONFIG_OF
  229. static struct of_regulator_match da9211_matches[] = {
  230. [DA9211_ID_BUCKA] = { .name = "BUCKA" },
  231. [DA9211_ID_BUCKB] = { .name = "BUCKB" },
  232. };
  233. static struct da9211_pdata *da9211_parse_regulators_dt(
  234. struct device *dev)
  235. {
  236. struct da9211_pdata *pdata;
  237. struct device_node *node;
  238. int i, num, n;
  239. node = of_get_child_by_name(dev->of_node, "regulators");
  240. if (!node) {
  241. dev_err(dev, "regulators node not found\n");
  242. return ERR_PTR(-ENODEV);
  243. }
  244. num = of_regulator_match(dev, node, da9211_matches,
  245. ARRAY_SIZE(da9211_matches));
  246. of_node_put(node);
  247. if (num < 0) {
  248. dev_err(dev, "Failed to match regulators\n");
  249. return ERR_PTR(-EINVAL);
  250. }
  251. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  252. if (!pdata)
  253. return ERR_PTR(-ENOMEM);
  254. pdata->num_buck = num;
  255. n = 0;
  256. for (i = 0; i < ARRAY_SIZE(da9211_matches); i++) {
  257. if (!da9211_matches[i].init_data)
  258. continue;
  259. pdata->init_data[n] = da9211_matches[i].init_data;
  260. pdata->reg_node[n] = da9211_matches[i].of_node;
  261. pdata->gpiod_ren[n] = devm_gpiod_get_from_of_node(dev,
  262. da9211_matches[i].of_node,
  263. "enable",
  264. 0,
  265. GPIOD_OUT_HIGH,
  266. "da9211-enable");
  267. n++;
  268. }
  269. return pdata;
  270. }
  271. #else
  272. static struct da9211_pdata *da9211_parse_regulators_dt(
  273. struct device *dev)
  274. {
  275. return ERR_PTR(-ENODEV);
  276. }
  277. #endif
  278. static irqreturn_t da9211_irq_handler(int irq, void *data)
  279. {
  280. struct da9211 *chip = data;
  281. int reg_val, err, ret = IRQ_NONE;
  282. err = regmap_read(chip->regmap, DA9211_REG_EVENT_B, &reg_val);
  283. if (err < 0)
  284. goto error_i2c;
  285. if (reg_val & DA9211_E_OV_CURR_A) {
  286. regulator_notifier_call_chain(chip->rdev[0],
  287. REGULATOR_EVENT_OVER_CURRENT, NULL);
  288. err = regmap_write(chip->regmap, DA9211_REG_EVENT_B,
  289. DA9211_E_OV_CURR_A);
  290. if (err < 0)
  291. goto error_i2c;
  292. ret = IRQ_HANDLED;
  293. }
  294. if (reg_val & DA9211_E_OV_CURR_B) {
  295. regulator_notifier_call_chain(chip->rdev[1],
  296. REGULATOR_EVENT_OVER_CURRENT, NULL);
  297. err = regmap_write(chip->regmap, DA9211_REG_EVENT_B,
  298. DA9211_E_OV_CURR_B);
  299. if (err < 0)
  300. goto error_i2c;
  301. ret = IRQ_HANDLED;
  302. }
  303. return ret;
  304. error_i2c:
  305. dev_err(chip->dev, "I2C error : %d\n", err);
  306. return IRQ_NONE;
  307. }
  308. static int da9211_regulator_init(struct da9211 *chip)
  309. {
  310. struct regulator_config config = { };
  311. int i, ret;
  312. unsigned int data;
  313. ret = regmap_read(chip->regmap, DA9211_REG_CONFIG_E, &data);
  314. if (ret < 0) {
  315. dev_err(chip->dev, "Failed to read CONFIG_E reg: %d\n", ret);
  316. return ret;
  317. }
  318. data &= DA9211_SLAVE_SEL;
  319. /* If configuration for 1/2 bucks is different between platform data
  320. * and the register, driver should exit.
  321. */
  322. if (chip->pdata->num_buck == 1 && data == 0x00)
  323. chip->num_regulator = 1;
  324. else if (chip->pdata->num_buck == 2 && data != 0x00)
  325. chip->num_regulator = 2;
  326. else {
  327. dev_err(chip->dev, "Configuration is mismatched\n");
  328. return -EINVAL;
  329. }
  330. for (i = 0; i < chip->num_regulator; i++) {
  331. config.init_data = chip->pdata->init_data[i];
  332. config.dev = chip->dev;
  333. config.driver_data = chip;
  334. config.regmap = chip->regmap;
  335. config.of_node = chip->pdata->reg_node[i];
  336. if (chip->pdata->gpiod_ren[i])
  337. config.ena_gpiod = chip->pdata->gpiod_ren[i];
  338. else
  339. config.ena_gpiod = NULL;
  340. chip->rdev[i] = devm_regulator_register(chip->dev,
  341. &da9211_regulators[i], &config);
  342. if (IS_ERR(chip->rdev[i])) {
  343. dev_err(chip->dev,
  344. "Failed to register DA9211 regulator\n");
  345. return PTR_ERR(chip->rdev[i]);
  346. }
  347. if (chip->chip_irq != 0) {
  348. ret = regmap_update_bits(chip->regmap,
  349. DA9211_REG_MASK_B, DA9211_M_OV_CURR_A << i, 0);
  350. if (ret < 0) {
  351. dev_err(chip->dev,
  352. "Failed to update mask reg: %d\n", ret);
  353. return ret;
  354. }
  355. }
  356. }
  357. return 0;
  358. }
  359. /*
  360. * I2C driver interface functions
  361. */
  362. static int da9211_i2c_probe(struct i2c_client *i2c,
  363. const struct i2c_device_id *id)
  364. {
  365. struct da9211 *chip;
  366. int error, ret;
  367. unsigned int data;
  368. chip = devm_kzalloc(&i2c->dev, sizeof(struct da9211), GFP_KERNEL);
  369. if (!chip)
  370. return -ENOMEM;
  371. chip->dev = &i2c->dev;
  372. chip->regmap = devm_regmap_init_i2c(i2c, &da9211_regmap_config);
  373. if (IS_ERR(chip->regmap)) {
  374. error = PTR_ERR(chip->regmap);
  375. dev_err(chip->dev, "Failed to allocate register map: %d\n",
  376. error);
  377. return error;
  378. }
  379. i2c_set_clientdata(i2c, chip);
  380. chip->pdata = i2c->dev.platform_data;
  381. ret = regmap_read(chip->regmap, DA9211_REG_DEVICE_ID, &data);
  382. if (ret < 0) {
  383. dev_err(chip->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
  384. return ret;
  385. }
  386. switch (data) {
  387. case DA9211_DEVICE_ID:
  388. chip->chip_id = DA9211;
  389. break;
  390. case DA9213_DEVICE_ID:
  391. chip->chip_id = DA9213;
  392. break;
  393. case DA9215_DEVICE_ID:
  394. chip->chip_id = DA9215;
  395. break;
  396. default:
  397. dev_err(chip->dev, "Unsupported device id = 0x%x.\n", data);
  398. return -ENODEV;
  399. }
  400. if (!chip->pdata)
  401. chip->pdata = da9211_parse_regulators_dt(chip->dev);
  402. if (IS_ERR(chip->pdata)) {
  403. dev_err(chip->dev, "No regulators defined for the platform\n");
  404. return PTR_ERR(chip->pdata);
  405. }
  406. chip->chip_irq = i2c->irq;
  407. if (chip->chip_irq != 0) {
  408. ret = devm_request_threaded_irq(chip->dev, chip->chip_irq, NULL,
  409. da9211_irq_handler,
  410. IRQF_TRIGGER_LOW|IRQF_ONESHOT,
  411. "da9211", chip);
  412. if (ret != 0) {
  413. dev_err(chip->dev, "Failed to request IRQ: %d\n",
  414. chip->chip_irq);
  415. return ret;
  416. }
  417. } else {
  418. dev_warn(chip->dev, "No IRQ configured\n");
  419. }
  420. ret = da9211_regulator_init(chip);
  421. if (ret < 0)
  422. dev_err(chip->dev, "Failed to initialize regulator: %d\n", ret);
  423. return ret;
  424. }
  425. static const struct i2c_device_id da9211_i2c_id[] = {
  426. {"da9211", DA9211},
  427. {"da9212", DA9212},
  428. {"da9213", DA9213},
  429. {"da9223", DA9223},
  430. {"da9214", DA9214},
  431. {"da9224", DA9224},
  432. {"da9215", DA9215},
  433. {"da9225", DA9225},
  434. {},
  435. };
  436. MODULE_DEVICE_TABLE(i2c, da9211_i2c_id);
  437. #ifdef CONFIG_OF
  438. static const struct of_device_id da9211_dt_ids[] = {
  439. { .compatible = "dlg,da9211", .data = &da9211_i2c_id[0] },
  440. { .compatible = "dlg,da9212", .data = &da9211_i2c_id[1] },
  441. { .compatible = "dlg,da9213", .data = &da9211_i2c_id[2] },
  442. { .compatible = "dlg,da9223", .data = &da9211_i2c_id[3] },
  443. { .compatible = "dlg,da9214", .data = &da9211_i2c_id[4] },
  444. { .compatible = "dlg,da9224", .data = &da9211_i2c_id[5] },
  445. { .compatible = "dlg,da9215", .data = &da9211_i2c_id[6] },
  446. { .compatible = "dlg,da9225", .data = &da9211_i2c_id[7] },
  447. {},
  448. };
  449. MODULE_DEVICE_TABLE(of, da9211_dt_ids);
  450. #endif
  451. static struct i2c_driver da9211_regulator_driver = {
  452. .driver = {
  453. .name = "da9211",
  454. .of_match_table = of_match_ptr(da9211_dt_ids),
  455. },
  456. .probe = da9211_i2c_probe,
  457. .id_table = da9211_i2c_id,
  458. };
  459. module_i2c_driver(da9211_regulator_driver);
  460. MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
  461. MODULE_DESCRIPTION("DA9211/DA9212/DA9213/DA9223/DA9214/DA9224/DA9215/DA9225 regulator driver");
  462. MODULE_LICENSE("GPL");