act8865-regulator.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
  4. *
  5. * http://www.active-semi.com/products/power-management-units/act88xx/
  6. *
  7. * Copyright (C) 2013 Atmel Corporation
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/i2c.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/act8865.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/regulator/of_regulator.h>
  20. #include <linux/regmap.h>
  21. #include <dt-bindings/regulator/active-semi,8865-regulator.h>
  22. /*
  23. * ACT8600 Global Register Map.
  24. */
  25. #define ACT8600_SYS_MODE 0x00
  26. #define ACT8600_SYS_CTRL 0x01
  27. #define ACT8600_DCDC1_VSET 0x10
  28. #define ACT8600_DCDC1_CTRL 0x12
  29. #define ACT8600_DCDC2_VSET 0x20
  30. #define ACT8600_DCDC2_CTRL 0x22
  31. #define ACT8600_DCDC3_VSET 0x30
  32. #define ACT8600_DCDC3_CTRL 0x32
  33. #define ACT8600_SUDCDC4_VSET 0x40
  34. #define ACT8600_SUDCDC4_CTRL 0x41
  35. #define ACT8600_LDO5_VSET 0x50
  36. #define ACT8600_LDO5_CTRL 0x51
  37. #define ACT8600_LDO6_VSET 0x60
  38. #define ACT8600_LDO6_CTRL 0x61
  39. #define ACT8600_LDO7_VSET 0x70
  40. #define ACT8600_LDO7_CTRL 0x71
  41. #define ACT8600_LDO8_VSET 0x80
  42. #define ACT8600_LDO8_CTRL 0x81
  43. #define ACT8600_LDO910_CTRL 0x91
  44. #define ACT8600_APCH0 0xA1
  45. #define ACT8600_APCH1 0xA8
  46. #define ACT8600_APCH2 0xA9
  47. #define ACT8600_APCH_STAT 0xAA
  48. #define ACT8600_OTG0 0xB0
  49. #define ACT8600_OTG1 0xB2
  50. /*
  51. * ACT8846 Global Register Map.
  52. */
  53. #define ACT8846_SYS0 0x00
  54. #define ACT8846_SYS1 0x01
  55. #define ACT8846_REG1_VSET 0x10
  56. #define ACT8846_REG1_CTRL 0x12
  57. #define ACT8846_REG2_VSET0 0x20
  58. #define ACT8846_REG2_VSET1 0x21
  59. #define ACT8846_REG2_CTRL 0x22
  60. #define ACT8846_REG3_VSET0 0x30
  61. #define ACT8846_REG3_VSET1 0x31
  62. #define ACT8846_REG3_CTRL 0x32
  63. #define ACT8846_REG4_VSET0 0x40
  64. #define ACT8846_REG4_VSET1 0x41
  65. #define ACT8846_REG4_CTRL 0x42
  66. #define ACT8846_REG5_VSET 0x50
  67. #define ACT8846_REG5_CTRL 0x51
  68. #define ACT8846_REG6_VSET 0x58
  69. #define ACT8846_REG6_CTRL 0x59
  70. #define ACT8846_REG7_VSET 0x60
  71. #define ACT8846_REG7_CTRL 0x61
  72. #define ACT8846_REG8_VSET 0x68
  73. #define ACT8846_REG8_CTRL 0x69
  74. #define ACT8846_REG9_VSET 0x70
  75. #define ACT8846_REG9_CTRL 0x71
  76. #define ACT8846_REG10_VSET 0x80
  77. #define ACT8846_REG10_CTRL 0x81
  78. #define ACT8846_REG11_VSET 0x90
  79. #define ACT8846_REG11_CTRL 0x91
  80. #define ACT8846_REG12_VSET 0xa0
  81. #define ACT8846_REG12_CTRL 0xa1
  82. #define ACT8846_REG13_CTRL 0xb1
  83. #define ACT8846_GLB_OFF_CTRL 0xc3
  84. #define ACT8846_OFF_SYSMASK 0x18
  85. /*
  86. * ACT8865 Global Register Map.
  87. */
  88. #define ACT8865_SYS_MODE 0x00
  89. #define ACT8865_SYS_CTRL 0x01
  90. #define ACT8865_SYS_UNLK_REGS 0x0b
  91. #define ACT8865_DCDC1_VSET1 0x20
  92. #define ACT8865_DCDC1_VSET2 0x21
  93. #define ACT8865_DCDC1_CTRL 0x22
  94. #define ACT8865_DCDC1_SUS 0x24
  95. #define ACT8865_DCDC2_VSET1 0x30
  96. #define ACT8865_DCDC2_VSET2 0x31
  97. #define ACT8865_DCDC2_CTRL 0x32
  98. #define ACT8865_DCDC2_SUS 0x34
  99. #define ACT8865_DCDC3_VSET1 0x40
  100. #define ACT8865_DCDC3_VSET2 0x41
  101. #define ACT8865_DCDC3_CTRL 0x42
  102. #define ACT8865_DCDC3_SUS 0x44
  103. #define ACT8865_LDO1_VSET 0x50
  104. #define ACT8865_LDO1_CTRL 0x51
  105. #define ACT8865_LDO1_SUS 0x52
  106. #define ACT8865_LDO2_VSET 0x54
  107. #define ACT8865_LDO2_CTRL 0x55
  108. #define ACT8865_LDO2_SUS 0x56
  109. #define ACT8865_LDO3_VSET 0x60
  110. #define ACT8865_LDO3_CTRL 0x61
  111. #define ACT8865_LDO3_SUS 0x62
  112. #define ACT8865_LDO4_VSET 0x64
  113. #define ACT8865_LDO4_CTRL 0x65
  114. #define ACT8865_LDO4_SUS 0x66
  115. #define ACT8865_MSTROFF 0x20
  116. /*
  117. * Field Definitions.
  118. */
  119. #define ACT8865_ENA 0x80 /* ON - [7] */
  120. #define ACT8865_DIS 0x40 /* DIS - [6] */
  121. #define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
  122. #define ACT8600_LDO10_ENA 0x40 /* ON - [6] */
  123. #define ACT8600_SUDCDC_VSEL_MASK 0xFF /* SUDCDC VSET - [7:0] */
  124. #define ACT8600_APCH_CHG_ACIN BIT(7)
  125. #define ACT8600_APCH_CHG_USB BIT(6)
  126. #define ACT8600_APCH_CSTATE0 BIT(5)
  127. #define ACT8600_APCH_CSTATE1 BIT(4)
  128. /*
  129. * ACT8865 voltage number
  130. */
  131. #define ACT8865_VOLTAGE_NUM 64
  132. #define ACT8600_SUDCDC_VOLTAGE_NUM 256
  133. struct act8865 {
  134. struct regmap *regmap;
  135. int off_reg;
  136. int off_mask;
  137. };
  138. static const struct regmap_range act8600_reg_ranges[] = {
  139. regmap_reg_range(0x00, 0x01),
  140. regmap_reg_range(0x10, 0x10),
  141. regmap_reg_range(0x12, 0x12),
  142. regmap_reg_range(0x20, 0x20),
  143. regmap_reg_range(0x22, 0x22),
  144. regmap_reg_range(0x30, 0x30),
  145. regmap_reg_range(0x32, 0x32),
  146. regmap_reg_range(0x40, 0x41),
  147. regmap_reg_range(0x50, 0x51),
  148. regmap_reg_range(0x60, 0x61),
  149. regmap_reg_range(0x70, 0x71),
  150. regmap_reg_range(0x80, 0x81),
  151. regmap_reg_range(0x91, 0x91),
  152. regmap_reg_range(0xA1, 0xA1),
  153. regmap_reg_range(0xA8, 0xAA),
  154. regmap_reg_range(0xB0, 0xB0),
  155. regmap_reg_range(0xB2, 0xB2),
  156. regmap_reg_range(0xC1, 0xC1),
  157. };
  158. static const struct regmap_range act8600_reg_ro_ranges[] = {
  159. regmap_reg_range(0xAA, 0xAA),
  160. regmap_reg_range(0xC1, 0xC1),
  161. };
  162. static const struct regmap_range act8600_reg_volatile_ranges[] = {
  163. regmap_reg_range(0x00, 0x01),
  164. regmap_reg_range(0x12, 0x12),
  165. regmap_reg_range(0x22, 0x22),
  166. regmap_reg_range(0x32, 0x32),
  167. regmap_reg_range(0x41, 0x41),
  168. regmap_reg_range(0x51, 0x51),
  169. regmap_reg_range(0x61, 0x61),
  170. regmap_reg_range(0x71, 0x71),
  171. regmap_reg_range(0x81, 0x81),
  172. regmap_reg_range(0xA8, 0xA8),
  173. regmap_reg_range(0xAA, 0xAA),
  174. regmap_reg_range(0xB0, 0xB0),
  175. regmap_reg_range(0xC1, 0xC1),
  176. };
  177. static const struct regmap_access_table act8600_write_ranges_table = {
  178. .yes_ranges = act8600_reg_ranges,
  179. .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
  180. .no_ranges = act8600_reg_ro_ranges,
  181. .n_no_ranges = ARRAY_SIZE(act8600_reg_ro_ranges),
  182. };
  183. static const struct regmap_access_table act8600_read_ranges_table = {
  184. .yes_ranges = act8600_reg_ranges,
  185. .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
  186. };
  187. static const struct regmap_access_table act8600_volatile_ranges_table = {
  188. .yes_ranges = act8600_reg_volatile_ranges,
  189. .n_yes_ranges = ARRAY_SIZE(act8600_reg_volatile_ranges),
  190. };
  191. static const struct regmap_config act8600_regmap_config = {
  192. .reg_bits = 8,
  193. .val_bits = 8,
  194. .max_register = 0xFF,
  195. .wr_table = &act8600_write_ranges_table,
  196. .rd_table = &act8600_read_ranges_table,
  197. .volatile_table = &act8600_volatile_ranges_table,
  198. };
  199. static const struct regmap_config act8865_regmap_config = {
  200. .reg_bits = 8,
  201. .val_bits = 8,
  202. };
  203. static const struct regulator_linear_range act8865_voltage_ranges[] = {
  204. REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
  205. REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
  206. REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
  207. };
  208. static const struct regulator_linear_range act8600_sudcdc_voltage_ranges[] = {
  209. REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
  210. REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
  211. REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
  212. REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000),
  213. REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0),
  214. };
  215. static int act8865_set_suspend_state(struct regulator_dev *rdev, bool enable)
  216. {
  217. struct regmap *regmap = rdev->regmap;
  218. int id = rdev->desc->id, reg, val;
  219. switch (id) {
  220. case ACT8865_ID_DCDC1:
  221. reg = ACT8865_DCDC1_SUS;
  222. val = 0xa8;
  223. break;
  224. case ACT8865_ID_DCDC2:
  225. reg = ACT8865_DCDC2_SUS;
  226. val = 0xa8;
  227. break;
  228. case ACT8865_ID_DCDC3:
  229. reg = ACT8865_DCDC3_SUS;
  230. val = 0xa8;
  231. break;
  232. case ACT8865_ID_LDO1:
  233. reg = ACT8865_LDO1_SUS;
  234. val = 0xe8;
  235. break;
  236. case ACT8865_ID_LDO2:
  237. reg = ACT8865_LDO2_SUS;
  238. val = 0xe8;
  239. break;
  240. case ACT8865_ID_LDO3:
  241. reg = ACT8865_LDO3_SUS;
  242. val = 0xe8;
  243. break;
  244. case ACT8865_ID_LDO4:
  245. reg = ACT8865_LDO4_SUS;
  246. val = 0xe8;
  247. break;
  248. default:
  249. return -EINVAL;
  250. }
  251. if (enable)
  252. val |= BIT(4);
  253. /*
  254. * Ask the PMIC to enable/disable this output when entering hibernate
  255. * mode.
  256. */
  257. return regmap_write(regmap, reg, val);
  258. }
  259. static int act8865_set_suspend_enable(struct regulator_dev *rdev)
  260. {
  261. return act8865_set_suspend_state(rdev, true);
  262. }
  263. static int act8865_set_suspend_disable(struct regulator_dev *rdev)
  264. {
  265. return act8865_set_suspend_state(rdev, false);
  266. }
  267. static unsigned int act8865_of_map_mode(unsigned int mode)
  268. {
  269. switch (mode) {
  270. case ACT8865_REGULATOR_MODE_FIXED:
  271. return REGULATOR_MODE_FAST;
  272. case ACT8865_REGULATOR_MODE_NORMAL:
  273. return REGULATOR_MODE_NORMAL;
  274. case ACT8865_REGULATOR_MODE_LOWPOWER:
  275. return REGULATOR_MODE_STANDBY;
  276. default:
  277. return REGULATOR_MODE_INVALID;
  278. }
  279. }
  280. static int act8865_set_mode(struct regulator_dev *rdev, unsigned int mode)
  281. {
  282. struct regmap *regmap = rdev->regmap;
  283. int id = rdev_get_id(rdev);
  284. int reg, val = 0;
  285. switch (id) {
  286. case ACT8865_ID_DCDC1:
  287. reg = ACT8865_DCDC1_CTRL;
  288. break;
  289. case ACT8865_ID_DCDC2:
  290. reg = ACT8865_DCDC2_CTRL;
  291. break;
  292. case ACT8865_ID_DCDC3:
  293. reg = ACT8865_DCDC3_CTRL;
  294. break;
  295. case ACT8865_ID_LDO1:
  296. reg = ACT8865_LDO1_CTRL;
  297. break;
  298. case ACT8865_ID_LDO2:
  299. reg = ACT8865_LDO2_CTRL;
  300. break;
  301. case ACT8865_ID_LDO3:
  302. reg = ACT8865_LDO3_CTRL;
  303. break;
  304. case ACT8865_ID_LDO4:
  305. reg = ACT8865_LDO4_CTRL;
  306. break;
  307. default:
  308. return -EINVAL;
  309. }
  310. switch (mode) {
  311. case REGULATOR_MODE_FAST:
  312. case REGULATOR_MODE_NORMAL:
  313. if (id <= ACT8865_ID_DCDC3)
  314. val = BIT(5);
  315. break;
  316. case REGULATOR_MODE_STANDBY:
  317. if (id > ACT8865_ID_DCDC3)
  318. val = BIT(5);
  319. break;
  320. default:
  321. return -EINVAL;
  322. }
  323. return regmap_update_bits(regmap, reg, BIT(5), val);
  324. }
  325. static unsigned int act8865_get_mode(struct regulator_dev *rdev)
  326. {
  327. struct regmap *regmap = rdev->regmap;
  328. int id = rdev_get_id(rdev);
  329. int reg, ret, val = 0;
  330. switch (id) {
  331. case ACT8865_ID_DCDC1:
  332. reg = ACT8865_DCDC1_CTRL;
  333. break;
  334. case ACT8865_ID_DCDC2:
  335. reg = ACT8865_DCDC2_CTRL;
  336. break;
  337. case ACT8865_ID_DCDC3:
  338. reg = ACT8865_DCDC3_CTRL;
  339. break;
  340. case ACT8865_ID_LDO1:
  341. reg = ACT8865_LDO1_CTRL;
  342. break;
  343. case ACT8865_ID_LDO2:
  344. reg = ACT8865_LDO2_CTRL;
  345. break;
  346. case ACT8865_ID_LDO3:
  347. reg = ACT8865_LDO3_CTRL;
  348. break;
  349. case ACT8865_ID_LDO4:
  350. reg = ACT8865_LDO4_CTRL;
  351. break;
  352. default:
  353. return -EINVAL;
  354. }
  355. ret = regmap_read(regmap, reg, &val);
  356. if (ret)
  357. return ret;
  358. if (id <= ACT8865_ID_DCDC3 && (val & BIT(5)))
  359. return REGULATOR_MODE_FAST;
  360. else if (id > ACT8865_ID_DCDC3 && !(val & BIT(5)))
  361. return REGULATOR_MODE_NORMAL;
  362. else
  363. return REGULATOR_MODE_STANDBY;
  364. }
  365. static const struct regulator_ops act8865_ops = {
  366. .list_voltage = regulator_list_voltage_linear_range,
  367. .map_voltage = regulator_map_voltage_linear_range,
  368. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  369. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  370. .enable = regulator_enable_regmap,
  371. .disable = regulator_disable_regmap,
  372. .set_mode = act8865_set_mode,
  373. .get_mode = act8865_get_mode,
  374. .is_enabled = regulator_is_enabled_regmap,
  375. .set_suspend_enable = act8865_set_suspend_enable,
  376. .set_suspend_disable = act8865_set_suspend_disable,
  377. };
  378. static const struct regulator_ops act8865_ldo_ops = {
  379. .list_voltage = regulator_list_voltage_linear_range,
  380. .map_voltage = regulator_map_voltage_linear_range,
  381. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  382. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  383. .enable = regulator_enable_regmap,
  384. .disable = regulator_disable_regmap,
  385. .set_mode = act8865_set_mode,
  386. .get_mode = act8865_get_mode,
  387. .is_enabled = regulator_is_enabled_regmap,
  388. .set_suspend_enable = act8865_set_suspend_enable,
  389. .set_suspend_disable = act8865_set_suspend_disable,
  390. .set_pull_down = regulator_set_pull_down_regmap,
  391. };
  392. static const struct regulator_ops act8865_fixed_ldo_ops = {
  393. .enable = regulator_enable_regmap,
  394. .disable = regulator_disable_regmap,
  395. .is_enabled = regulator_is_enabled_regmap,
  396. };
  397. #define ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, _ops) \
  398. [_family##_ID_##_id] = { \
  399. .name = _name, \
  400. .of_match = of_match_ptr(_name), \
  401. .of_map_mode = act8865_of_map_mode, \
  402. .regulators_node = of_match_ptr("regulators"), \
  403. .supply_name = _supply, \
  404. .id = _family##_ID_##_id, \
  405. .type = REGULATOR_VOLTAGE, \
  406. .ops = _ops, \
  407. .n_voltages = ACT8865_VOLTAGE_NUM, \
  408. .linear_ranges = act8865_voltage_ranges, \
  409. .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
  410. .vsel_reg = _family##_##_id##_##_vsel_reg, \
  411. .vsel_mask = ACT8865_VSEL_MASK, \
  412. .enable_reg = _family##_##_id##_CTRL, \
  413. .enable_mask = ACT8865_ENA, \
  414. .pull_down_reg = _family##_##_id##_CTRL, \
  415. .pull_down_mask = ACT8865_DIS, \
  416. .owner = THIS_MODULE, \
  417. }
  418. #define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply) \
  419. ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ops)
  420. #define ACT88xx_LDO(_name, _family, _id, _vsel_reg, _supply) \
  421. ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ldo_ops)
  422. static const struct regulator_desc act8600_regulators[] = {
  423. ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
  424. ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
  425. ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
  426. {
  427. .name = "SUDCDC_REG4",
  428. .of_match = of_match_ptr("SUDCDC_REG4"),
  429. .regulators_node = of_match_ptr("regulators"),
  430. .id = ACT8600_ID_SUDCDC4,
  431. .ops = &act8865_ops,
  432. .type = REGULATOR_VOLTAGE,
  433. .n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
  434. .linear_ranges = act8600_sudcdc_voltage_ranges,
  435. .n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
  436. .vsel_reg = ACT8600_SUDCDC4_VSET,
  437. .vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
  438. .enable_reg = ACT8600_SUDCDC4_CTRL,
  439. .enable_mask = ACT8865_ENA,
  440. .owner = THIS_MODULE,
  441. },
  442. ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
  443. ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
  444. ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
  445. ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
  446. {
  447. .name = "LDO_REG9",
  448. .of_match = of_match_ptr("LDO_REG9"),
  449. .regulators_node = of_match_ptr("regulators"),
  450. .id = ACT8600_ID_LDO9,
  451. .ops = &act8865_fixed_ldo_ops,
  452. .type = REGULATOR_VOLTAGE,
  453. .n_voltages = 1,
  454. .fixed_uV = 3300000,
  455. .enable_reg = ACT8600_LDO910_CTRL,
  456. .enable_mask = ACT8865_ENA,
  457. .owner = THIS_MODULE,
  458. },
  459. {
  460. .name = "LDO_REG10",
  461. .of_match = of_match_ptr("LDO_REG10"),
  462. .regulators_node = of_match_ptr("regulators"),
  463. .id = ACT8600_ID_LDO10,
  464. .ops = &act8865_fixed_ldo_ops,
  465. .type = REGULATOR_VOLTAGE,
  466. .n_voltages = 1,
  467. .fixed_uV = 1200000,
  468. .enable_reg = ACT8600_LDO910_CTRL,
  469. .enable_mask = ACT8600_LDO10_ENA,
  470. .owner = THIS_MODULE,
  471. },
  472. };
  473. static const struct regulator_desc act8846_regulators[] = {
  474. ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
  475. ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
  476. ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
  477. ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
  478. ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
  479. ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
  480. ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
  481. ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
  482. ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
  483. ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
  484. ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
  485. ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
  486. };
  487. static const struct regulator_desc act8865_regulators[] = {
  488. ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
  489. ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
  490. ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
  491. ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
  492. ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
  493. ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
  494. ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
  495. };
  496. static const struct regulator_desc act8865_alt_regulators[] = {
  497. ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"),
  498. ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"),
  499. ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"),
  500. ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
  501. ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
  502. ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
  503. ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
  504. };
  505. #ifdef CONFIG_OF
  506. static const struct of_device_id act8865_dt_ids[] = {
  507. { .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
  508. { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
  509. { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
  510. { }
  511. };
  512. MODULE_DEVICE_TABLE(of, act8865_dt_ids);
  513. #endif
  514. static struct act8865_regulator_data *act8865_get_regulator_data(
  515. int id, struct act8865_platform_data *pdata)
  516. {
  517. int i;
  518. for (i = 0; i < pdata->num_regulators; i++) {
  519. if (pdata->regulators[i].id == id)
  520. return &pdata->regulators[i];
  521. }
  522. return NULL;
  523. }
  524. static struct i2c_client *act8865_i2c_client;
  525. static void act8865_power_off(void)
  526. {
  527. struct act8865 *act8865;
  528. act8865 = i2c_get_clientdata(act8865_i2c_client);
  529. regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
  530. while (1);
  531. }
  532. static int act8600_charger_get_status(struct regmap *map)
  533. {
  534. unsigned int val;
  535. int ret;
  536. u8 state0, state1;
  537. ret = regmap_read(map, ACT8600_APCH_STAT, &val);
  538. if (ret < 0)
  539. return ret;
  540. state0 = val & ACT8600_APCH_CSTATE0;
  541. state1 = val & ACT8600_APCH_CSTATE1;
  542. if (state0 && !state1)
  543. return POWER_SUPPLY_STATUS_CHARGING;
  544. if (!state0 && state1)
  545. return POWER_SUPPLY_STATUS_NOT_CHARGING;
  546. if (!state0 && !state1)
  547. return POWER_SUPPLY_STATUS_DISCHARGING;
  548. return POWER_SUPPLY_STATUS_UNKNOWN;
  549. }
  550. static int act8600_charger_get_property(struct power_supply *psy,
  551. enum power_supply_property psp, union power_supply_propval *val)
  552. {
  553. struct regmap *map = power_supply_get_drvdata(psy);
  554. int ret;
  555. switch (psp) {
  556. case POWER_SUPPLY_PROP_STATUS:
  557. ret = act8600_charger_get_status(map);
  558. if (ret < 0)
  559. return ret;
  560. val->intval = ret;
  561. break;
  562. default:
  563. return -EINVAL;
  564. }
  565. return 0;
  566. }
  567. static enum power_supply_property act8600_charger_properties[] = {
  568. POWER_SUPPLY_PROP_STATUS,
  569. };
  570. static const struct power_supply_desc act8600_charger_desc = {
  571. .name = "act8600-charger",
  572. .type = POWER_SUPPLY_TYPE_BATTERY,
  573. .properties = act8600_charger_properties,
  574. .num_properties = ARRAY_SIZE(act8600_charger_properties),
  575. .get_property = act8600_charger_get_property,
  576. };
  577. static int act8600_charger_probe(struct device *dev, struct regmap *regmap)
  578. {
  579. struct power_supply *charger;
  580. struct power_supply_config cfg = {
  581. .drv_data = regmap,
  582. .of_node = dev->of_node,
  583. };
  584. charger = devm_power_supply_register(dev, &act8600_charger_desc, &cfg);
  585. return PTR_ERR_OR_ZERO(charger);
  586. }
  587. static int act8865_pmic_probe(struct i2c_client *client,
  588. const struct i2c_device_id *i2c_id)
  589. {
  590. const struct regulator_desc *regulators;
  591. struct act8865_platform_data *pdata = NULL;
  592. struct device *dev = &client->dev;
  593. int i, ret, num_regulators;
  594. struct act8865 *act8865;
  595. const struct regmap_config *regmap_config;
  596. unsigned long type;
  597. int off_reg, off_mask;
  598. int voltage_select = 0;
  599. if (dev->of_node) {
  600. const struct of_device_id *id;
  601. id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
  602. if (!id)
  603. return -ENODEV;
  604. type = (unsigned long) id->data;
  605. voltage_select = !!of_get_property(dev->of_node,
  606. "active-semi,vsel-high",
  607. NULL);
  608. } else {
  609. type = i2c_id->driver_data;
  610. pdata = dev_get_platdata(dev);
  611. }
  612. switch (type) {
  613. case ACT8600:
  614. regulators = act8600_regulators;
  615. num_regulators = ARRAY_SIZE(act8600_regulators);
  616. regmap_config = &act8600_regmap_config;
  617. off_reg = -1;
  618. off_mask = -1;
  619. break;
  620. case ACT8846:
  621. regulators = act8846_regulators;
  622. num_regulators = ARRAY_SIZE(act8846_regulators);
  623. regmap_config = &act8865_regmap_config;
  624. off_reg = ACT8846_GLB_OFF_CTRL;
  625. off_mask = ACT8846_OFF_SYSMASK;
  626. break;
  627. case ACT8865:
  628. if (voltage_select) {
  629. regulators = act8865_alt_regulators;
  630. num_regulators = ARRAY_SIZE(act8865_alt_regulators);
  631. } else {
  632. regulators = act8865_regulators;
  633. num_regulators = ARRAY_SIZE(act8865_regulators);
  634. }
  635. regmap_config = &act8865_regmap_config;
  636. off_reg = ACT8865_SYS_CTRL;
  637. off_mask = ACT8865_MSTROFF;
  638. break;
  639. default:
  640. dev_err(dev, "invalid device id %lu\n", type);
  641. return -EINVAL;
  642. }
  643. act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
  644. if (!act8865)
  645. return -ENOMEM;
  646. act8865->regmap = devm_regmap_init_i2c(client, regmap_config);
  647. if (IS_ERR(act8865->regmap)) {
  648. ret = PTR_ERR(act8865->regmap);
  649. dev_err(dev, "Failed to allocate register map: %d\n", ret);
  650. return ret;
  651. }
  652. if (of_device_is_system_power_controller(dev->of_node)) {
  653. if (!pm_power_off && (off_reg > 0)) {
  654. act8865_i2c_client = client;
  655. act8865->off_reg = off_reg;
  656. act8865->off_mask = off_mask;
  657. pm_power_off = act8865_power_off;
  658. } else {
  659. dev_err(dev, "Failed to set poweroff capability, already defined\n");
  660. }
  661. }
  662. /* Finally register devices */
  663. for (i = 0; i < num_regulators; i++) {
  664. const struct regulator_desc *desc = &regulators[i];
  665. struct regulator_config config = { };
  666. struct regulator_dev *rdev;
  667. config.dev = dev;
  668. config.driver_data = act8865;
  669. config.regmap = act8865->regmap;
  670. if (pdata) {
  671. struct act8865_regulator_data *rdata;
  672. rdata = act8865_get_regulator_data(desc->id, pdata);
  673. if (rdata) {
  674. config.init_data = rdata->init_data;
  675. config.of_node = rdata->of_node;
  676. }
  677. }
  678. rdev = devm_regulator_register(dev, desc, &config);
  679. if (IS_ERR(rdev)) {
  680. dev_err(dev, "failed to register %s\n", desc->name);
  681. return PTR_ERR(rdev);
  682. }
  683. }
  684. if (type == ACT8600) {
  685. ret = act8600_charger_probe(dev, act8865->regmap);
  686. if (ret < 0) {
  687. if (ret != -EPROBE_DEFER)
  688. dev_err(dev, "Failed to probe charger");
  689. return ret;
  690. }
  691. }
  692. i2c_set_clientdata(client, act8865);
  693. /* Unlock expert registers for ACT8865. */
  694. return type != ACT8865 ? 0 : regmap_write(act8865->regmap,
  695. ACT8865_SYS_UNLK_REGS, 0xef);
  696. }
  697. static const struct i2c_device_id act8865_ids[] = {
  698. { .name = "act8600", .driver_data = ACT8600 },
  699. { .name = "act8846", .driver_data = ACT8846 },
  700. { .name = "act8865", .driver_data = ACT8865 },
  701. { },
  702. };
  703. MODULE_DEVICE_TABLE(i2c, act8865_ids);
  704. static struct i2c_driver act8865_pmic_driver = {
  705. .driver = {
  706. .name = "act8865",
  707. },
  708. .probe = act8865_pmic_probe,
  709. .id_table = act8865_ids,
  710. };
  711. module_i2c_driver(act8865_pmic_driver);
  712. MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
  713. MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
  714. MODULE_LICENSE("GPL v2");