act8865-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #define ACT8846_GLB_OFF_CTRL 0xc3
  63. #define ACT8846_OFF_SYSMASK 0x18
  64. /*
  65. * ACT8865 Global Register Map.
  66. */
  67. #define ACT8865_SYS_MODE 0x00
  68. #define ACT8865_SYS_CTRL 0x01
  69. #define ACT8865_DCDC1_VSET1 0x20
  70. #define ACT8865_DCDC1_VSET2 0x21
  71. #define ACT8865_DCDC1_CTRL 0x22
  72. #define ACT8865_DCDC2_VSET1 0x30
  73. #define ACT8865_DCDC2_VSET2 0x31
  74. #define ACT8865_DCDC2_CTRL 0x32
  75. #define ACT8865_DCDC3_VSET1 0x40
  76. #define ACT8865_DCDC3_VSET2 0x41
  77. #define ACT8865_DCDC3_CTRL 0x42
  78. #define ACT8865_LDO1_VSET 0x50
  79. #define ACT8865_LDO1_CTRL 0x51
  80. #define ACT8865_LDO2_VSET 0x54
  81. #define ACT8865_LDO2_CTRL 0x55
  82. #define ACT8865_LDO3_VSET 0x60
  83. #define ACT8865_LDO3_CTRL 0x61
  84. #define ACT8865_LDO4_VSET 0x64
  85. #define ACT8865_LDO4_CTRL 0x65
  86. #define ACT8865_MSTROFF 0x20
  87. /*
  88. * Field Definitions.
  89. */
  90. #define ACT8865_ENA 0x80 /* ON - [7] */
  91. #define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
  92. /*
  93. * ACT8865 voltage number
  94. */
  95. #define ACT8865_VOLTAGE_NUM 64
  96. struct act8865 {
  97. struct regmap *regmap;
  98. int off_reg;
  99. int off_mask;
  100. };
  101. static const struct regmap_config act8865_regmap_config = {
  102. .reg_bits = 8,
  103. .val_bits = 8,
  104. };
  105. static const struct regulator_linear_range act8865_voltage_ranges[] = {
  106. REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
  107. REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
  108. REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
  109. };
  110. static struct regulator_ops act8865_ops = {
  111. .list_voltage = regulator_list_voltage_linear_range,
  112. .map_voltage = regulator_map_voltage_linear_range,
  113. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  114. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  115. .enable = regulator_enable_regmap,
  116. .disable = regulator_disable_regmap,
  117. .is_enabled = regulator_is_enabled_regmap,
  118. };
  119. #define ACT88xx_REG(_name, _family, _id, _vsel_reg) \
  120. [_family##_ID_##_id] = { \
  121. .name = _name, \
  122. .id = _family##_ID_##_id, \
  123. .type = REGULATOR_VOLTAGE, \
  124. .ops = &act8865_ops, \
  125. .n_voltages = ACT8865_VOLTAGE_NUM, \
  126. .linear_ranges = act8865_voltage_ranges, \
  127. .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
  128. .vsel_reg = _family##_##_id##_##_vsel_reg, \
  129. .vsel_mask = ACT8865_VSEL_MASK, \
  130. .enable_reg = _family##_##_id##_CTRL, \
  131. .enable_mask = ACT8865_ENA, \
  132. .owner = THIS_MODULE, \
  133. }
  134. static const struct regulator_desc act8846_regulators[] = {
  135. ACT88xx_REG("REG1", ACT8846, REG1, VSET),
  136. ACT88xx_REG("REG2", ACT8846, REG2, VSET0),
  137. ACT88xx_REG("REG3", ACT8846, REG3, VSET0),
  138. ACT88xx_REG("REG4", ACT8846, REG4, VSET0),
  139. ACT88xx_REG("REG5", ACT8846, REG5, VSET),
  140. ACT88xx_REG("REG6", ACT8846, REG6, VSET),
  141. ACT88xx_REG("REG7", ACT8846, REG7, VSET),
  142. ACT88xx_REG("REG8", ACT8846, REG8, VSET),
  143. ACT88xx_REG("REG9", ACT8846, REG9, VSET),
  144. ACT88xx_REG("REG10", ACT8846, REG10, VSET),
  145. ACT88xx_REG("REG11", ACT8846, REG11, VSET),
  146. ACT88xx_REG("REG12", ACT8846, REG12, VSET),
  147. };
  148. static const struct regulator_desc act8865_regulators[] = {
  149. ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1),
  150. ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1),
  151. ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1),
  152. ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET),
  153. ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET),
  154. ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET),
  155. ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET),
  156. };
  157. #ifdef CONFIG_OF
  158. static const struct of_device_id act8865_dt_ids[] = {
  159. { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
  160. { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(of, act8865_dt_ids);
  164. static struct of_regulator_match act8846_matches[] = {
  165. [ACT8846_ID_REG1] = { .name = "REG1" },
  166. [ACT8846_ID_REG2] = { .name = "REG2" },
  167. [ACT8846_ID_REG3] = { .name = "REG3" },
  168. [ACT8846_ID_REG4] = { .name = "REG4" },
  169. [ACT8846_ID_REG5] = { .name = "REG5" },
  170. [ACT8846_ID_REG6] = { .name = "REG6" },
  171. [ACT8846_ID_REG7] = { .name = "REG7" },
  172. [ACT8846_ID_REG8] = { .name = "REG8" },
  173. [ACT8846_ID_REG9] = { .name = "REG9" },
  174. [ACT8846_ID_REG10] = { .name = "REG10" },
  175. [ACT8846_ID_REG11] = { .name = "REG11" },
  176. [ACT8846_ID_REG12] = { .name = "REG12" },
  177. };
  178. static struct of_regulator_match act8865_matches[] = {
  179. [ACT8865_ID_DCDC1] = { .name = "DCDC_REG1"},
  180. [ACT8865_ID_DCDC2] = { .name = "DCDC_REG2"},
  181. [ACT8865_ID_DCDC3] = { .name = "DCDC_REG3"},
  182. [ACT8865_ID_LDO1] = { .name = "LDO_REG1"},
  183. [ACT8865_ID_LDO2] = { .name = "LDO_REG2"},
  184. [ACT8865_ID_LDO3] = { .name = "LDO_REG3"},
  185. [ACT8865_ID_LDO4] = { .name = "LDO_REG4"},
  186. };
  187. static int act8865_pdata_from_dt(struct device *dev,
  188. struct device_node **of_node,
  189. struct act8865_platform_data *pdata,
  190. unsigned long type)
  191. {
  192. int matched, i, num_matches;
  193. struct device_node *np;
  194. struct act8865_regulator_data *regulator;
  195. struct of_regulator_match *matches;
  196. np = of_get_child_by_name(dev->of_node, "regulators");
  197. if (!np) {
  198. dev_err(dev, "missing 'regulators' subnode in DT\n");
  199. return -EINVAL;
  200. }
  201. switch (type) {
  202. case ACT8846:
  203. matches = act8846_matches;
  204. num_matches = ARRAY_SIZE(act8846_matches);
  205. break;
  206. case ACT8865:
  207. matches = act8865_matches;
  208. num_matches = ARRAY_SIZE(act8865_matches);
  209. break;
  210. default:
  211. dev_err(dev, "invalid device id %lu\n", type);
  212. return -EINVAL;
  213. }
  214. matched = of_regulator_match(dev, np, matches, num_matches);
  215. of_node_put(np);
  216. if (matched <= 0)
  217. return matched;
  218. pdata->regulators = devm_kzalloc(dev,
  219. sizeof(struct act8865_regulator_data) *
  220. num_matches, GFP_KERNEL);
  221. if (!pdata->regulators)
  222. return -ENOMEM;
  223. pdata->num_regulators = num_matches;
  224. regulator = pdata->regulators;
  225. for (i = 0; i < num_matches; i++) {
  226. regulator->id = i;
  227. regulator->name = matches[i].name;
  228. regulator->platform_data = matches[i].init_data;
  229. of_node[i] = matches[i].of_node;
  230. regulator++;
  231. }
  232. return 0;
  233. }
  234. #else
  235. static inline int act8865_pdata_from_dt(struct device *dev,
  236. struct device_node **of_node,
  237. struct act8865_platform_data *pdata,
  238. unsigned long type)
  239. {
  240. return 0;
  241. }
  242. #endif
  243. static struct regulator_init_data
  244. *act8865_get_init_data(int id, struct act8865_platform_data *pdata)
  245. {
  246. int i;
  247. if (!pdata)
  248. return NULL;
  249. for (i = 0; i < pdata->num_regulators; i++) {
  250. if (pdata->regulators[i].id == id)
  251. return pdata->regulators[i].platform_data;
  252. }
  253. return NULL;
  254. }
  255. static struct i2c_client *act8865_i2c_client;
  256. static void act8865_power_off(void)
  257. {
  258. struct act8865 *act8865;
  259. act8865 = i2c_get_clientdata(act8865_i2c_client);
  260. regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
  261. while (1);
  262. }
  263. static int act8865_pmic_probe(struct i2c_client *client,
  264. const struct i2c_device_id *i2c_id)
  265. {
  266. static const struct regulator_desc *regulators;
  267. struct act8865_platform_data pdata_of, *pdata;
  268. struct device *dev = &client->dev;
  269. struct device_node **of_node;
  270. int i, ret, num_regulators;
  271. struct act8865 *act8865;
  272. unsigned long type;
  273. int off_reg, off_mask;
  274. pdata = dev_get_platdata(dev);
  275. if (dev->of_node && !pdata) {
  276. const struct of_device_id *id;
  277. id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
  278. if (!id)
  279. return -ENODEV;
  280. type = (unsigned long) id->data;
  281. } else {
  282. type = i2c_id->driver_data;
  283. }
  284. switch (type) {
  285. case ACT8846:
  286. regulators = act8846_regulators;
  287. num_regulators = ARRAY_SIZE(act8846_regulators);
  288. off_reg = ACT8846_GLB_OFF_CTRL;
  289. off_mask = ACT8846_OFF_SYSMASK;
  290. break;
  291. case ACT8865:
  292. regulators = act8865_regulators;
  293. num_regulators = ARRAY_SIZE(act8865_regulators);
  294. off_reg = ACT8865_SYS_CTRL;
  295. off_mask = ACT8865_MSTROFF;
  296. break;
  297. default:
  298. dev_err(dev, "invalid device id %lu\n", type);
  299. return -EINVAL;
  300. }
  301. of_node = devm_kzalloc(dev, sizeof(struct device_node *) *
  302. num_regulators, GFP_KERNEL);
  303. if (!of_node)
  304. return -ENOMEM;
  305. if (dev->of_node && !pdata) {
  306. ret = act8865_pdata_from_dt(dev, of_node, &pdata_of, type);
  307. if (ret < 0)
  308. return ret;
  309. pdata = &pdata_of;
  310. }
  311. if (pdata->num_regulators > num_regulators) {
  312. dev_err(dev, "too many regulators: %d\n",
  313. pdata->num_regulators);
  314. return -EINVAL;
  315. }
  316. act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
  317. if (!act8865)
  318. return -ENOMEM;
  319. act8865->regmap = devm_regmap_init_i2c(client, &act8865_regmap_config);
  320. if (IS_ERR(act8865->regmap)) {
  321. ret = PTR_ERR(act8865->regmap);
  322. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  323. ret);
  324. return ret;
  325. }
  326. if (of_device_is_system_power_controller(dev->of_node)) {
  327. if (!pm_power_off) {
  328. act8865_i2c_client = client;
  329. act8865->off_reg = off_reg;
  330. act8865->off_mask = off_mask;
  331. pm_power_off = act8865_power_off;
  332. } else {
  333. dev_err(dev, "Failed to set poweroff capability, already defined\n");
  334. }
  335. }
  336. /* Finally register devices */
  337. for (i = 0; i < num_regulators; i++) {
  338. const struct regulator_desc *desc = &regulators[i];
  339. struct regulator_config config = { };
  340. struct regulator_dev *rdev;
  341. config.dev = dev;
  342. config.init_data = act8865_get_init_data(desc->id, pdata);
  343. config.of_node = of_node[i];
  344. config.driver_data = act8865;
  345. config.regmap = act8865->regmap;
  346. rdev = devm_regulator_register(&client->dev, desc, &config);
  347. if (IS_ERR(rdev)) {
  348. dev_err(dev, "failed to register %s\n", desc->name);
  349. return PTR_ERR(rdev);
  350. }
  351. }
  352. i2c_set_clientdata(client, act8865);
  353. devm_kfree(dev, of_node);
  354. return 0;
  355. }
  356. static const struct i2c_device_id act8865_ids[] = {
  357. { .name = "act8846", .driver_data = ACT8846 },
  358. { .name = "act8865", .driver_data = ACT8865 },
  359. { },
  360. };
  361. MODULE_DEVICE_TABLE(i2c, act8865_ids);
  362. static struct i2c_driver act8865_pmic_driver = {
  363. .driver = {
  364. .name = "act8865",
  365. .owner = THIS_MODULE,
  366. },
  367. .probe = act8865_pmic_probe,
  368. .id_table = act8865_ids,
  369. };
  370. module_i2c_driver(act8865_pmic_driver);
  371. MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
  372. MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
  373. MODULE_LICENSE("GPL v2");