act8865-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
  3. *
  4. * http://www.active-semi.com/products/power-management-units/act88xx/
  5. *
  6. * Copyright (C) 2013 Atmel Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/i2c.h>
  21. #include <linux/err.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/act8865.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/regulator/of_regulator.h>
  28. #include <linux/regmap.h>
  29. /*
  30. * ACT8846 Global Register Map.
  31. */
  32. #define ACT8846_SYS0 0x00
  33. #define ACT8846_SYS1 0x01
  34. #define ACT8846_REG1_VSET 0x10
  35. #define ACT8846_REG1_CTRL 0x12
  36. #define ACT8846_REG2_VSET0 0x20
  37. #define ACT8846_REG2_VSET1 0x21
  38. #define ACT8846_REG2_CTRL 0x22
  39. #define ACT8846_REG3_VSET0 0x30
  40. #define ACT8846_REG3_VSET1 0x31
  41. #define ACT8846_REG3_CTRL 0x32
  42. #define ACT8846_REG4_VSET0 0x40
  43. #define ACT8846_REG4_VSET1 0x41
  44. #define ACT8846_REG4_CTRL 0x42
  45. #define ACT8846_REG5_VSET 0x50
  46. #define ACT8846_REG5_CTRL 0x51
  47. #define ACT8846_REG6_VSET 0x58
  48. #define ACT8846_REG6_CTRL 0x59
  49. #define ACT8846_REG7_VSET 0x60
  50. #define ACT8846_REG7_CTRL 0x61
  51. #define ACT8846_REG8_VSET 0x68
  52. #define ACT8846_REG8_CTRL 0x69
  53. #define ACT8846_REG9_VSET 0x70
  54. #define ACT8846_REG9_CTRL 0x71
  55. #define ACT8846_REG10_VSET 0x80
  56. #define ACT8846_REG10_CTRL 0x81
  57. #define ACT8846_REG11_VSET 0x90
  58. #define ACT8846_REG11_CTRL 0x91
  59. #define ACT8846_REG12_VSET 0xa0
  60. #define ACT8846_REG12_CTRL 0xa1
  61. #define ACT8846_REG13_CTRL 0xb1
  62. /*
  63. * ACT8865 Global Register Map.
  64. */
  65. #define ACT8865_SYS_MODE 0x00
  66. #define ACT8865_SYS_CTRL 0x01
  67. #define ACT8865_DCDC1_VSET1 0x20
  68. #define ACT8865_DCDC1_VSET2 0x21
  69. #define ACT8865_DCDC1_CTRL 0x22
  70. #define ACT8865_DCDC2_VSET1 0x30
  71. #define ACT8865_DCDC2_VSET2 0x31
  72. #define ACT8865_DCDC2_CTRL 0x32
  73. #define ACT8865_DCDC3_VSET1 0x40
  74. #define ACT8865_DCDC3_VSET2 0x41
  75. #define ACT8865_DCDC3_CTRL 0x42
  76. #define ACT8865_LDO1_VSET 0x50
  77. #define ACT8865_LDO1_CTRL 0x51
  78. #define ACT8865_LDO2_VSET 0x54
  79. #define ACT8865_LDO2_CTRL 0x55
  80. #define ACT8865_LDO3_VSET 0x60
  81. #define ACT8865_LDO3_CTRL 0x61
  82. #define ACT8865_LDO4_VSET 0x64
  83. #define ACT8865_LDO4_CTRL 0x65
  84. /*
  85. * Field Definitions.
  86. */
  87. #define ACT8865_ENA 0x80 /* ON - [7] */
  88. #define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
  89. /*
  90. * ACT8865 voltage number
  91. */
  92. #define ACT8865_VOLTAGE_NUM 64
  93. struct act8865 {
  94. struct regmap *regmap;
  95. };
  96. static const struct regmap_config act8865_regmap_config = {
  97. .reg_bits = 8,
  98. .val_bits = 8,
  99. };
  100. static const struct regulator_linear_range act8865_voltage_ranges[] = {
  101. REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
  102. REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
  103. REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
  104. };
  105. static struct regulator_ops act8865_ops = {
  106. .list_voltage = regulator_list_voltage_linear_range,
  107. .map_voltage = regulator_map_voltage_linear_range,
  108. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  109. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  110. .enable = regulator_enable_regmap,
  111. .disable = regulator_disable_regmap,
  112. .is_enabled = regulator_is_enabled_regmap,
  113. };
  114. #define ACT88xx_REG(_name, _family, _id, _vsel_reg) \
  115. [_family##_ID_##_id] = { \
  116. .name = _name, \
  117. .id = _family##_ID_##_id, \
  118. .type = REGULATOR_VOLTAGE, \
  119. .ops = &act8865_ops, \
  120. .n_voltages = ACT8865_VOLTAGE_NUM, \
  121. .linear_ranges = act8865_voltage_ranges, \
  122. .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
  123. .vsel_reg = _family##_##_id##_##_vsel_reg, \
  124. .vsel_mask = ACT8865_VSEL_MASK, \
  125. .enable_reg = _family##_##_id##_CTRL, \
  126. .enable_mask = ACT8865_ENA, \
  127. .owner = THIS_MODULE, \
  128. }
  129. static const struct regulator_desc act8846_regulators[] = {
  130. ACT88xx_REG("REG1", ACT8846, REG1, VSET),
  131. ACT88xx_REG("REG2", ACT8846, REG2, VSET0),
  132. ACT88xx_REG("REG3", ACT8846, REG3, VSET0),
  133. ACT88xx_REG("REG4", ACT8846, REG4, VSET0),
  134. ACT88xx_REG("REG5", ACT8846, REG5, VSET),
  135. ACT88xx_REG("REG6", ACT8846, REG6, VSET),
  136. ACT88xx_REG("REG7", ACT8846, REG7, VSET),
  137. ACT88xx_REG("REG8", ACT8846, REG8, VSET),
  138. ACT88xx_REG("REG9", ACT8846, REG9, VSET),
  139. ACT88xx_REG("REG10", ACT8846, REG10, VSET),
  140. ACT88xx_REG("REG11", ACT8846, REG11, VSET),
  141. ACT88xx_REG("REG12", ACT8846, REG12, VSET),
  142. };
  143. static const struct regulator_desc act8865_regulators[] = {
  144. ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1),
  145. ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1),
  146. ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1),
  147. ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET),
  148. ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET),
  149. ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET),
  150. ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET),
  151. };
  152. #ifdef CONFIG_OF
  153. static const struct of_device_id act8865_dt_ids[] = {
  154. { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
  155. { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
  156. { }
  157. };
  158. MODULE_DEVICE_TABLE(of, act8865_dt_ids);
  159. static struct of_regulator_match act8846_matches[] = {
  160. [ACT8846_ID_REG1] = { .name = "REG1" },
  161. [ACT8846_ID_REG2] = { .name = "REG2" },
  162. [ACT8846_ID_REG3] = { .name = "REG3" },
  163. [ACT8846_ID_REG4] = { .name = "REG4" },
  164. [ACT8846_ID_REG5] = { .name = "REG5" },
  165. [ACT8846_ID_REG6] = { .name = "REG6" },
  166. [ACT8846_ID_REG7] = { .name = "REG7" },
  167. [ACT8846_ID_REG8] = { .name = "REG8" },
  168. [ACT8846_ID_REG9] = { .name = "REG9" },
  169. [ACT8846_ID_REG10] = { .name = "REG10" },
  170. [ACT8846_ID_REG11] = { .name = "REG11" },
  171. [ACT8846_ID_REG12] = { .name = "REG12" },
  172. };
  173. static struct of_regulator_match act8865_matches[] = {
  174. [ACT8865_ID_DCDC1] = { .name = "DCDC_REG1"},
  175. [ACT8865_ID_DCDC2] = { .name = "DCDC_REG2"},
  176. [ACT8865_ID_DCDC3] = { .name = "DCDC_REG3"},
  177. [ACT8865_ID_LDO1] = { .name = "LDO_REG1"},
  178. [ACT8865_ID_LDO2] = { .name = "LDO_REG2"},
  179. [ACT8865_ID_LDO3] = { .name = "LDO_REG3"},
  180. [ACT8865_ID_LDO4] = { .name = "LDO_REG4"},
  181. };
  182. static int act8865_pdata_from_dt(struct device *dev,
  183. struct device_node **of_node,
  184. struct act8865_platform_data *pdata,
  185. unsigned long type)
  186. {
  187. int matched, i, num_matches;
  188. struct device_node *np;
  189. struct act8865_regulator_data *regulator;
  190. struct of_regulator_match *matches;
  191. np = of_get_child_by_name(dev->of_node, "regulators");
  192. if (!np) {
  193. dev_err(dev, "missing 'regulators' subnode in DT\n");
  194. return -EINVAL;
  195. }
  196. switch (type) {
  197. case ACT8846:
  198. matches = act8846_matches;
  199. num_matches = ARRAY_SIZE(act8846_matches);
  200. break;
  201. case ACT8865:
  202. matches = act8865_matches;
  203. num_matches = ARRAY_SIZE(act8865_matches);
  204. break;
  205. default:
  206. dev_err(dev, "invalid device id %lu\n", type);
  207. return -EINVAL;
  208. }
  209. matched = of_regulator_match(dev, np, matches, num_matches);
  210. of_node_put(np);
  211. if (matched <= 0)
  212. return matched;
  213. pdata->regulators = devm_kzalloc(dev,
  214. sizeof(struct act8865_regulator_data) *
  215. num_matches, GFP_KERNEL);
  216. if (!pdata->regulators)
  217. return -ENOMEM;
  218. pdata->num_regulators = num_matches;
  219. regulator = pdata->regulators;
  220. for (i = 0; i < num_matches; i++) {
  221. regulator->id = i;
  222. regulator->name = matches[i].name;
  223. regulator->platform_data = matches[i].init_data;
  224. of_node[i] = matches[i].of_node;
  225. regulator++;
  226. }
  227. return 0;
  228. }
  229. #else
  230. static inline int act8865_pdata_from_dt(struct device *dev,
  231. struct device_node **of_node,
  232. struct act8865_platform_data *pdata,
  233. unsigned long type)
  234. {
  235. return 0;
  236. }
  237. #endif
  238. static struct regulator_init_data
  239. *act8865_get_init_data(int id, struct act8865_platform_data *pdata)
  240. {
  241. int i;
  242. if (!pdata)
  243. return NULL;
  244. for (i = 0; i < pdata->num_regulators; i++) {
  245. if (pdata->regulators[i].id == id)
  246. return pdata->regulators[i].platform_data;
  247. }
  248. return NULL;
  249. }
  250. static int act8865_pmic_probe(struct i2c_client *client,
  251. const struct i2c_device_id *i2c_id)
  252. {
  253. static const struct regulator_desc *regulators;
  254. struct act8865_platform_data pdata_of, *pdata;
  255. struct device *dev = &client->dev;
  256. struct device_node **of_node;
  257. int i, ret, num_regulators;
  258. struct act8865 *act8865;
  259. unsigned long type;
  260. pdata = dev_get_platdata(dev);
  261. if (dev->of_node && !pdata) {
  262. const struct of_device_id *id;
  263. id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
  264. if (!id)
  265. return -ENODEV;
  266. type = (unsigned long) id->data;
  267. } else {
  268. type = i2c_id->driver_data;
  269. }
  270. switch (type) {
  271. case ACT8846:
  272. regulators = act8846_regulators;
  273. num_regulators = ARRAY_SIZE(act8846_regulators);
  274. break;
  275. case ACT8865:
  276. regulators = act8865_regulators;
  277. num_regulators = ARRAY_SIZE(act8865_regulators);
  278. break;
  279. default:
  280. dev_err(dev, "invalid device id %lu\n", type);
  281. return -EINVAL;
  282. }
  283. of_node = devm_kzalloc(dev, sizeof(struct device_node *) *
  284. num_regulators, GFP_KERNEL);
  285. if (!of_node)
  286. return -ENOMEM;
  287. if (dev->of_node && !pdata) {
  288. ret = act8865_pdata_from_dt(dev, of_node, &pdata_of, type);
  289. if (ret < 0)
  290. return ret;
  291. pdata = &pdata_of;
  292. }
  293. if (pdata->num_regulators > num_regulators) {
  294. dev_err(dev, "too many regulators: %d\n",
  295. pdata->num_regulators);
  296. return -EINVAL;
  297. }
  298. act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
  299. if (!act8865)
  300. return -ENOMEM;
  301. act8865->regmap = devm_regmap_init_i2c(client, &act8865_regmap_config);
  302. if (IS_ERR(act8865->regmap)) {
  303. ret = PTR_ERR(act8865->regmap);
  304. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  305. ret);
  306. return ret;
  307. }
  308. /* Finally register devices */
  309. for (i = 0; i < num_regulators; i++) {
  310. const struct regulator_desc *desc = &regulators[i];
  311. struct regulator_config config = { };
  312. struct regulator_dev *rdev;
  313. config.dev = dev;
  314. config.init_data = act8865_get_init_data(desc->id, pdata);
  315. config.of_node = of_node[i];
  316. config.driver_data = act8865;
  317. config.regmap = act8865->regmap;
  318. rdev = devm_regulator_register(&client->dev, desc, &config);
  319. if (IS_ERR(rdev)) {
  320. dev_err(dev, "failed to register %s\n", desc->name);
  321. return PTR_ERR(rdev);
  322. }
  323. }
  324. i2c_set_clientdata(client, act8865);
  325. devm_kfree(dev, of_node);
  326. return 0;
  327. }
  328. static const struct i2c_device_id act8865_ids[] = {
  329. { .name = "act8846", .driver_data = ACT8846 },
  330. { .name = "act8865", .driver_data = ACT8865 },
  331. { },
  332. };
  333. MODULE_DEVICE_TABLE(i2c, act8865_ids);
  334. static struct i2c_driver act8865_pmic_driver = {
  335. .driver = {
  336. .name = "act8865",
  337. .owner = THIS_MODULE,
  338. },
  339. .probe = act8865_pmic_probe,
  340. .id_table = act8865_ids,
  341. };
  342. module_i2c_driver(act8865_pmic_driver);
  343. MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
  344. MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
  345. MODULE_LICENSE("GPL v2");