da9211-regulator.c 13 KB

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