pfuze100-regulator.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/err.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/regulator/of_regulator.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reboot.h>
  13. #include <linux/regulator/driver.h>
  14. #include <linux/regulator/machine.h>
  15. #include <linux/regulator/pfuze100.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.h>
  18. #include <linux/regmap.h>
  19. #define PFUZE_FLAG_DISABLE_SW BIT(1)
  20. #define PFUZE_NUMREGS 128
  21. #define PFUZE100_VOL_OFFSET 0
  22. #define PFUZE100_STANDBY_OFFSET 1
  23. #define PFUZE100_MODE_OFFSET 3
  24. #define PFUZE100_CONF_OFFSET 4
  25. #define PFUZE100_DEVICEID 0x0
  26. #define PFUZE100_REVID 0x3
  27. #define PFUZE100_FABID 0x4
  28. #define PFUZE100_COINVOL 0x1a
  29. #define PFUZE100_SW1ABVOL 0x20
  30. #define PFUZE100_SW1ABMODE 0x23
  31. #define PFUZE100_SW1CVOL 0x2e
  32. #define PFUZE100_SW1CMODE 0x31
  33. #define PFUZE100_SW2VOL 0x35
  34. #define PFUZE100_SW2MODE 0x38
  35. #define PFUZE100_SW3AVOL 0x3c
  36. #define PFUZE100_SW3AMODE 0x3f
  37. #define PFUZE100_SW3BVOL 0x43
  38. #define PFUZE100_SW3BMODE 0x46
  39. #define PFUZE100_SW4VOL 0x4a
  40. #define PFUZE100_SW4MODE 0x4d
  41. #define PFUZE100_SWBSTCON1 0x66
  42. #define PFUZE100_VREFDDRCON 0x6a
  43. #define PFUZE100_VSNVSVOL 0x6b
  44. #define PFUZE100_VGEN1VOL 0x6c
  45. #define PFUZE100_VGEN2VOL 0x6d
  46. #define PFUZE100_VGEN3VOL 0x6e
  47. #define PFUZE100_VGEN4VOL 0x6f
  48. #define PFUZE100_VGEN5VOL 0x70
  49. #define PFUZE100_VGEN6VOL 0x71
  50. #define PFUZE100_SWxMODE_MASK 0xf
  51. #define PFUZE100_SWxMODE_APS_APS 0x8
  52. #define PFUZE100_SWxMODE_APS_OFF 0x4
  53. #define PFUZE100_VGENxLPWR BIT(6)
  54. #define PFUZE100_VGENxSTBY BIT(5)
  55. enum chips { PFUZE100, PFUZE200, PFUZE3000 = 3, PFUZE3001 = 0x31, };
  56. struct pfuze_regulator {
  57. struct regulator_desc desc;
  58. unsigned char stby_reg;
  59. unsigned char stby_mask;
  60. bool sw_reg;
  61. };
  62. struct pfuze_chip {
  63. int chip_id;
  64. int flags;
  65. struct regmap *regmap;
  66. struct device *dev;
  67. struct pfuze_regulator regulator_descs[PFUZE100_MAX_REGULATOR];
  68. struct regulator_dev *regulators[PFUZE100_MAX_REGULATOR];
  69. struct pfuze_regulator *pfuze_regulators;
  70. };
  71. static const int pfuze100_swbst[] = {
  72. 5000000, 5050000, 5100000, 5150000,
  73. };
  74. static const int pfuze100_vsnvs[] = {
  75. 1000000, 1100000, 1200000, 1300000, 1500000, 1800000, 3000000,
  76. };
  77. static const int pfuze100_coin[] = {
  78. 2500000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
  79. };
  80. static const int pfuze3000_sw1a[] = {
  81. 700000, 725000, 750000, 775000, 800000, 825000, 850000, 875000,
  82. 900000, 925000, 950000, 975000, 1000000, 1025000, 1050000, 1075000,
  83. 1100000, 1125000, 1150000, 1175000, 1200000, 1225000, 1250000, 1275000,
  84. 1300000, 1325000, 1350000, 1375000, 1400000, 1425000, 1800000, 3300000,
  85. };
  86. static const int pfuze3000_sw2lo[] = {
  87. 1500000, 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000,
  88. };
  89. static const int pfuze3000_sw2hi[] = {
  90. 2500000, 2800000, 2850000, 3000000, 3100000, 3150000, 3200000, 3300000,
  91. };
  92. static const struct of_device_id pfuze_dt_ids[] = {
  93. { .compatible = "fsl,pfuze100", .data = (void *)PFUZE100},
  94. { .compatible = "fsl,pfuze200", .data = (void *)PFUZE200},
  95. { .compatible = "fsl,pfuze3000", .data = (void *)PFUZE3000},
  96. { .compatible = "fsl,pfuze3001", .data = (void *)PFUZE3001},
  97. { }
  98. };
  99. MODULE_DEVICE_TABLE(of, pfuze_dt_ids);
  100. static int pfuze100_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  101. {
  102. struct pfuze_chip *pfuze100 = rdev_get_drvdata(rdev);
  103. int id = rdev_get_id(rdev);
  104. bool reg_has_ramp_delay;
  105. unsigned int ramp_bits = 0;
  106. int ret;
  107. switch (pfuze100->chip_id) {
  108. case PFUZE3001:
  109. /* no dynamic voltage scaling for PF3001 */
  110. reg_has_ramp_delay = false;
  111. break;
  112. case PFUZE3000:
  113. reg_has_ramp_delay = (id < PFUZE3000_SWBST);
  114. break;
  115. case PFUZE200:
  116. reg_has_ramp_delay = (id < PFUZE200_SWBST);
  117. break;
  118. case PFUZE100:
  119. default:
  120. reg_has_ramp_delay = (id < PFUZE100_SWBST);
  121. break;
  122. }
  123. if (reg_has_ramp_delay) {
  124. if (ramp_delay > 0) {
  125. ramp_delay = 12500 / ramp_delay;
  126. ramp_bits = (ramp_delay >> 1) - (ramp_delay >> 3);
  127. }
  128. ret = regmap_update_bits(pfuze100->regmap,
  129. rdev->desc->vsel_reg + 4,
  130. 0xc0, ramp_bits << 6);
  131. if (ret < 0)
  132. dev_err(pfuze100->dev, "ramp failed, err %d\n", ret);
  133. } else {
  134. ret = -EACCES;
  135. }
  136. return ret;
  137. }
  138. static const struct regulator_ops pfuze100_ldo_regulator_ops = {
  139. .enable = regulator_enable_regmap,
  140. .disable = regulator_disable_regmap,
  141. .is_enabled = regulator_is_enabled_regmap,
  142. .list_voltage = regulator_list_voltage_linear,
  143. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  144. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  145. };
  146. static const struct regulator_ops pfuze100_fixed_regulator_ops = {
  147. .enable = regulator_enable_regmap,
  148. .disable = regulator_disable_regmap,
  149. .is_enabled = regulator_is_enabled_regmap,
  150. .list_voltage = regulator_list_voltage_linear,
  151. };
  152. static const struct regulator_ops pfuze100_sw_regulator_ops = {
  153. .list_voltage = regulator_list_voltage_linear,
  154. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  155. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  156. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  157. .set_ramp_delay = pfuze100_set_ramp_delay,
  158. };
  159. static const struct regulator_ops pfuze100_sw_disable_regulator_ops = {
  160. .enable = regulator_enable_regmap,
  161. .disable = regulator_disable_regmap,
  162. .is_enabled = regulator_is_enabled_regmap,
  163. .list_voltage = regulator_list_voltage_linear,
  164. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  165. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  166. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  167. .set_ramp_delay = pfuze100_set_ramp_delay,
  168. };
  169. static const struct regulator_ops pfuze100_swb_regulator_ops = {
  170. .enable = regulator_enable_regmap,
  171. .disable = regulator_disable_regmap,
  172. .is_enabled = regulator_is_enabled_regmap,
  173. .list_voltage = regulator_list_voltage_table,
  174. .map_voltage = regulator_map_voltage_ascend,
  175. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  176. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  177. };
  178. static const struct regulator_ops pfuze3000_sw_regulator_ops = {
  179. .enable = regulator_enable_regmap,
  180. .disable = regulator_disable_regmap,
  181. .is_enabled = regulator_is_enabled_regmap,
  182. .list_voltage = regulator_list_voltage_table,
  183. .map_voltage = regulator_map_voltage_ascend,
  184. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  185. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  186. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  187. .set_ramp_delay = pfuze100_set_ramp_delay,
  188. };
  189. #define PFUZE100_FIXED_REG(_chip, _name, base, voltage) \
  190. [_chip ## _ ## _name] = { \
  191. .desc = { \
  192. .name = #_name, \
  193. .n_voltages = 1, \
  194. .ops = &pfuze100_fixed_regulator_ops, \
  195. .type = REGULATOR_VOLTAGE, \
  196. .id = _chip ## _ ## _name, \
  197. .owner = THIS_MODULE, \
  198. .min_uV = (voltage), \
  199. .enable_reg = (base), \
  200. .enable_mask = 0x10, \
  201. }, \
  202. }
  203. #define PFUZE100_SW_REG(_chip, _name, base, min, max, step) \
  204. [_chip ## _ ## _name] = { \
  205. .desc = { \
  206. .name = #_name,\
  207. .n_voltages = ((max) - (min)) / (step) + 1, \
  208. .ops = &pfuze100_sw_regulator_ops, \
  209. .type = REGULATOR_VOLTAGE, \
  210. .id = _chip ## _ ## _name, \
  211. .owner = THIS_MODULE, \
  212. .min_uV = (min), \
  213. .uV_step = (step), \
  214. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  215. .vsel_mask = 0x3f, \
  216. .enable_reg = (base) + PFUZE100_MODE_OFFSET, \
  217. .enable_mask = 0xf, \
  218. }, \
  219. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  220. .stby_mask = 0x3f, \
  221. .sw_reg = true, \
  222. }
  223. #define PFUZE100_SWB_REG(_chip, _name, base, mask, voltages) \
  224. [_chip ## _ ## _name] = { \
  225. .desc = { \
  226. .name = #_name, \
  227. .n_voltages = ARRAY_SIZE(voltages), \
  228. .ops = &pfuze100_swb_regulator_ops, \
  229. .type = REGULATOR_VOLTAGE, \
  230. .id = _chip ## _ ## _name, \
  231. .owner = THIS_MODULE, \
  232. .volt_table = voltages, \
  233. .vsel_reg = (base), \
  234. .vsel_mask = (mask), \
  235. .enable_reg = (base), \
  236. .enable_mask = 0x48, \
  237. }, \
  238. }
  239. #define PFUZE100_VGEN_REG(_chip, _name, base, min, max, step) \
  240. [_chip ## _ ## _name] = { \
  241. .desc = { \
  242. .name = #_name, \
  243. .n_voltages = ((max) - (min)) / (step) + 1, \
  244. .ops = &pfuze100_ldo_regulator_ops, \
  245. .type = REGULATOR_VOLTAGE, \
  246. .id = _chip ## _ ## _name, \
  247. .owner = THIS_MODULE, \
  248. .min_uV = (min), \
  249. .uV_step = (step), \
  250. .vsel_reg = (base), \
  251. .vsel_mask = 0xf, \
  252. .enable_reg = (base), \
  253. .enable_mask = 0x10, \
  254. }, \
  255. .stby_reg = (base), \
  256. .stby_mask = 0x20, \
  257. }
  258. #define PFUZE100_COIN_REG(_chip, _name, base, mask, voltages) \
  259. [_chip ## _ ## _name] = { \
  260. .desc = { \
  261. .name = #_name, \
  262. .n_voltages = ARRAY_SIZE(voltages), \
  263. .ops = &pfuze100_swb_regulator_ops, \
  264. .type = REGULATOR_VOLTAGE, \
  265. .id = _chip ## _ ## _name, \
  266. .owner = THIS_MODULE, \
  267. .volt_table = voltages, \
  268. .vsel_reg = (base), \
  269. .vsel_mask = (mask), \
  270. .enable_reg = (base), \
  271. .enable_mask = 0x8, \
  272. }, \
  273. }
  274. #define PFUZE3000_VCC_REG(_chip, _name, base, min, max, step) { \
  275. .desc = { \
  276. .name = #_name, \
  277. .n_voltages = ((max) - (min)) / (step) + 1, \
  278. .ops = &pfuze100_ldo_regulator_ops, \
  279. .type = REGULATOR_VOLTAGE, \
  280. .id = _chip ## _ ## _name, \
  281. .owner = THIS_MODULE, \
  282. .min_uV = (min), \
  283. .uV_step = (step), \
  284. .vsel_reg = (base), \
  285. .vsel_mask = 0x3, \
  286. .enable_reg = (base), \
  287. .enable_mask = 0x10, \
  288. }, \
  289. .stby_reg = (base), \
  290. .stby_mask = 0x20, \
  291. }
  292. /* No linar case for the some switches of PFUZE3000 */
  293. #define PFUZE3000_SW_REG(_chip, _name, base, mask, voltages) \
  294. [_chip ## _ ## _name] = { \
  295. .desc = { \
  296. .name = #_name, \
  297. .n_voltages = ARRAY_SIZE(voltages), \
  298. .ops = &pfuze3000_sw_regulator_ops, \
  299. .type = REGULATOR_VOLTAGE, \
  300. .id = _chip ## _ ## _name, \
  301. .owner = THIS_MODULE, \
  302. .volt_table = voltages, \
  303. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  304. .vsel_mask = (mask), \
  305. .enable_reg = (base) + PFUZE100_MODE_OFFSET, \
  306. .enable_mask = 0xf, \
  307. .enable_val = 0x8, \
  308. .enable_time = 500, \
  309. }, \
  310. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  311. .stby_mask = (mask), \
  312. .sw_reg = true, \
  313. }
  314. #define PFUZE3000_SW3_REG(_chip, _name, base, min, max, step) { \
  315. .desc = { \
  316. .name = #_name,\
  317. .n_voltages = ((max) - (min)) / (step) + 1, \
  318. .ops = &pfuze100_sw_regulator_ops, \
  319. .type = REGULATOR_VOLTAGE, \
  320. .id = _chip ## _ ## _name, \
  321. .owner = THIS_MODULE, \
  322. .min_uV = (min), \
  323. .uV_step = (step), \
  324. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  325. .vsel_mask = 0xf, \
  326. }, \
  327. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  328. .stby_mask = 0xf, \
  329. }
  330. /* PFUZE100 */
  331. static struct pfuze_regulator pfuze100_regulators[] = {
  332. PFUZE100_SW_REG(PFUZE100, SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  333. PFUZE100_SW_REG(PFUZE100, SW1C, PFUZE100_SW1CVOL, 300000, 1875000, 25000),
  334. PFUZE100_SW_REG(PFUZE100, SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  335. PFUZE100_SW_REG(PFUZE100, SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  336. PFUZE100_SW_REG(PFUZE100, SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  337. PFUZE100_SW_REG(PFUZE100, SW4, PFUZE100_SW4VOL, 400000, 1975000, 25000),
  338. PFUZE100_SWB_REG(PFUZE100, SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  339. PFUZE100_SWB_REG(PFUZE100, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  340. PFUZE100_FIXED_REG(PFUZE100, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  341. PFUZE100_VGEN_REG(PFUZE100, VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  342. PFUZE100_VGEN_REG(PFUZE100, VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  343. PFUZE100_VGEN_REG(PFUZE100, VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  344. PFUZE100_VGEN_REG(PFUZE100, VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  345. PFUZE100_VGEN_REG(PFUZE100, VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  346. PFUZE100_VGEN_REG(PFUZE100, VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  347. PFUZE100_COIN_REG(PFUZE100, COIN, PFUZE100_COINVOL, 0x7, pfuze100_coin),
  348. };
  349. static struct pfuze_regulator pfuze200_regulators[] = {
  350. PFUZE100_SW_REG(PFUZE200, SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  351. PFUZE100_SW_REG(PFUZE200, SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  352. PFUZE100_SW_REG(PFUZE200, SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  353. PFUZE100_SW_REG(PFUZE200, SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  354. PFUZE100_SWB_REG(PFUZE200, SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  355. PFUZE100_SWB_REG(PFUZE200, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  356. PFUZE100_FIXED_REG(PFUZE200, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  357. PFUZE100_VGEN_REG(PFUZE200, VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  358. PFUZE100_VGEN_REG(PFUZE200, VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  359. PFUZE100_VGEN_REG(PFUZE200, VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  360. PFUZE100_VGEN_REG(PFUZE200, VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  361. PFUZE100_VGEN_REG(PFUZE200, VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  362. PFUZE100_VGEN_REG(PFUZE200, VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  363. PFUZE100_COIN_REG(PFUZE200, COIN, PFUZE100_COINVOL, 0x7, pfuze100_coin),
  364. };
  365. static struct pfuze_regulator pfuze3000_regulators[] = {
  366. PFUZE3000_SW_REG(PFUZE3000, SW1A, PFUZE100_SW1ABVOL, 0x1f, pfuze3000_sw1a),
  367. PFUZE100_SW_REG(PFUZE3000, SW1B, PFUZE100_SW1CVOL, 700000, 1475000, 25000),
  368. PFUZE3000_SW_REG(PFUZE3000, SW2, PFUZE100_SW2VOL, 0x7, pfuze3000_sw2lo),
  369. PFUZE3000_SW3_REG(PFUZE3000, SW3, PFUZE100_SW3AVOL, 900000, 1650000, 50000),
  370. PFUZE100_SWB_REG(PFUZE3000, SWBST, PFUZE100_SWBSTCON1, 0x3, pfuze100_swbst),
  371. PFUZE100_SWB_REG(PFUZE3000, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  372. PFUZE100_FIXED_REG(PFUZE3000, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  373. PFUZE100_VGEN_REG(PFUZE3000, VLDO1, PFUZE100_VGEN1VOL, 1800000, 3300000, 100000),
  374. PFUZE100_VGEN_REG(PFUZE3000, VLDO2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  375. PFUZE3000_VCC_REG(PFUZE3000, VCCSD, PFUZE100_VGEN3VOL, 2850000, 3300000, 150000),
  376. PFUZE3000_VCC_REG(PFUZE3000, V33, PFUZE100_VGEN4VOL, 2850000, 3300000, 150000),
  377. PFUZE100_VGEN_REG(PFUZE3000, VLDO3, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  378. PFUZE100_VGEN_REG(PFUZE3000, VLDO4, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  379. };
  380. static struct pfuze_regulator pfuze3001_regulators[] = {
  381. PFUZE3000_SW_REG(PFUZE3001, SW1, PFUZE100_SW1ABVOL, 0x1f, pfuze3000_sw1a),
  382. PFUZE3000_SW_REG(PFUZE3001, SW2, PFUZE100_SW2VOL, 0x7, pfuze3000_sw2lo),
  383. PFUZE3000_SW3_REG(PFUZE3001, SW3, PFUZE100_SW3AVOL, 900000, 1650000, 50000),
  384. PFUZE100_SWB_REG(PFUZE3001, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  385. PFUZE100_VGEN_REG(PFUZE3001, VLDO1, PFUZE100_VGEN1VOL, 1800000, 3300000, 100000),
  386. PFUZE100_VGEN_REG(PFUZE3001, VLDO2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  387. PFUZE3000_VCC_REG(PFUZE3001, VCCSD, PFUZE100_VGEN3VOL, 2850000, 3300000, 150000),
  388. PFUZE3000_VCC_REG(PFUZE3001, V33, PFUZE100_VGEN4VOL, 2850000, 3300000, 150000),
  389. PFUZE100_VGEN_REG(PFUZE3001, VLDO3, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  390. PFUZE100_VGEN_REG(PFUZE3001, VLDO4, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  391. };
  392. /* PFUZE100 */
  393. static struct of_regulator_match pfuze100_matches[] = {
  394. { .name = "sw1ab", },
  395. { .name = "sw1c", },
  396. { .name = "sw2", },
  397. { .name = "sw3a", },
  398. { .name = "sw3b", },
  399. { .name = "sw4", },
  400. { .name = "swbst", },
  401. { .name = "vsnvs", },
  402. { .name = "vrefddr", },
  403. { .name = "vgen1", },
  404. { .name = "vgen2", },
  405. { .name = "vgen3", },
  406. { .name = "vgen4", },
  407. { .name = "vgen5", },
  408. { .name = "vgen6", },
  409. { .name = "coin", },
  410. };
  411. /* PFUZE200 */
  412. static struct of_regulator_match pfuze200_matches[] = {
  413. { .name = "sw1ab", },
  414. { .name = "sw2", },
  415. { .name = "sw3a", },
  416. { .name = "sw3b", },
  417. { .name = "swbst", },
  418. { .name = "vsnvs", },
  419. { .name = "vrefddr", },
  420. { .name = "vgen1", },
  421. { .name = "vgen2", },
  422. { .name = "vgen3", },
  423. { .name = "vgen4", },
  424. { .name = "vgen5", },
  425. { .name = "vgen6", },
  426. { .name = "coin", },
  427. };
  428. /* PFUZE3000 */
  429. static struct of_regulator_match pfuze3000_matches[] = {
  430. { .name = "sw1a", },
  431. { .name = "sw1b", },
  432. { .name = "sw2", },
  433. { .name = "sw3", },
  434. { .name = "swbst", },
  435. { .name = "vsnvs", },
  436. { .name = "vrefddr", },
  437. { .name = "vldo1", },
  438. { .name = "vldo2", },
  439. { .name = "vccsd", },
  440. { .name = "v33", },
  441. { .name = "vldo3", },
  442. { .name = "vldo4", },
  443. };
  444. /* PFUZE3001 */
  445. static struct of_regulator_match pfuze3001_matches[] = {
  446. { .name = "sw1", },
  447. { .name = "sw2", },
  448. { .name = "sw3", },
  449. { .name = "vsnvs", },
  450. { .name = "vldo1", },
  451. { .name = "vldo2", },
  452. { .name = "vccsd", },
  453. { .name = "v33", },
  454. { .name = "vldo3", },
  455. { .name = "vldo4", },
  456. };
  457. static struct of_regulator_match *pfuze_matches;
  458. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  459. {
  460. struct device *dev = chip->dev;
  461. struct device_node *np, *parent;
  462. int ret;
  463. np = of_node_get(dev->of_node);
  464. if (!np)
  465. return -EINVAL;
  466. if (of_property_read_bool(np, "fsl,pfuze-support-disable-sw"))
  467. chip->flags |= PFUZE_FLAG_DISABLE_SW;
  468. parent = of_get_child_by_name(np, "regulators");
  469. if (!parent) {
  470. dev_err(dev, "regulators node not found\n");
  471. of_node_put(np);
  472. return -EINVAL;
  473. }
  474. switch (chip->chip_id) {
  475. case PFUZE3001:
  476. pfuze_matches = pfuze3001_matches;
  477. ret = of_regulator_match(dev, parent, pfuze3001_matches,
  478. ARRAY_SIZE(pfuze3001_matches));
  479. break;
  480. case PFUZE3000:
  481. pfuze_matches = pfuze3000_matches;
  482. ret = of_regulator_match(dev, parent, pfuze3000_matches,
  483. ARRAY_SIZE(pfuze3000_matches));
  484. break;
  485. case PFUZE200:
  486. pfuze_matches = pfuze200_matches;
  487. ret = of_regulator_match(dev, parent, pfuze200_matches,
  488. ARRAY_SIZE(pfuze200_matches));
  489. break;
  490. case PFUZE100:
  491. default:
  492. pfuze_matches = pfuze100_matches;
  493. ret = of_regulator_match(dev, parent, pfuze100_matches,
  494. ARRAY_SIZE(pfuze100_matches));
  495. break;
  496. }
  497. of_node_put(parent);
  498. of_node_put(np);
  499. if (ret < 0) {
  500. dev_err(dev, "Error parsing regulator init data: %d\n",
  501. ret);
  502. return ret;
  503. }
  504. return 0;
  505. }
  506. static inline struct regulator_init_data *match_init_data(int index)
  507. {
  508. return pfuze_matches[index].init_data;
  509. }
  510. static inline struct device_node *match_of_node(int index)
  511. {
  512. return pfuze_matches[index].of_node;
  513. }
  514. static int pfuze_power_off_prepare(struct sys_off_data *data)
  515. {
  516. struct pfuze_chip *syspm_pfuze_chip = data->cb_data;
  517. dev_info(syspm_pfuze_chip->dev, "Configure standby mode for power off");
  518. /* Switch from default mode: APS/APS to APS/Off */
  519. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW1ABMODE,
  520. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  521. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW1CMODE,
  522. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  523. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW2MODE,
  524. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  525. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW3AMODE,
  526. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  527. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW3BMODE,
  528. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  529. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW4MODE,
  530. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  531. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN1VOL,
  532. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  533. PFUZE100_VGENxSTBY);
  534. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN2VOL,
  535. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  536. PFUZE100_VGENxSTBY);
  537. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN3VOL,
  538. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  539. PFUZE100_VGENxSTBY);
  540. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN4VOL,
  541. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  542. PFUZE100_VGENxSTBY);
  543. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN5VOL,
  544. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  545. PFUZE100_VGENxSTBY);
  546. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN6VOL,
  547. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  548. PFUZE100_VGENxSTBY);
  549. return NOTIFY_DONE;
  550. }
  551. static int pfuze_power_off_prepare_init(struct pfuze_chip *pfuze_chip)
  552. {
  553. int err;
  554. if (pfuze_chip->chip_id != PFUZE100) {
  555. dev_warn(pfuze_chip->dev, "Requested pm_power_off_prepare handler for not supported chip\n");
  556. return -ENODEV;
  557. }
  558. err = devm_register_sys_off_handler(pfuze_chip->dev,
  559. SYS_OFF_MODE_POWER_OFF_PREPARE,
  560. SYS_OFF_PRIO_DEFAULT,
  561. pfuze_power_off_prepare,
  562. pfuze_chip);
  563. if (err) {
  564. dev_err(pfuze_chip->dev, "failed to register sys-off handler: %d\n",
  565. err);
  566. return err;
  567. }
  568. return 0;
  569. }
  570. static int pfuze_identify(struct pfuze_chip *pfuze_chip)
  571. {
  572. unsigned int value;
  573. int ret;
  574. ret = regmap_read(pfuze_chip->regmap, PFUZE100_DEVICEID, &value);
  575. if (ret)
  576. return ret;
  577. if (((value & 0x0f) == 0x8) && (pfuze_chip->chip_id == PFUZE100)) {
  578. /*
  579. * Freescale misprogrammed 1-3% of parts prior to week 8 of 2013
  580. * as ID=8 in PFUZE100
  581. */
  582. dev_info(pfuze_chip->dev, "Assuming misprogrammed ID=0x8");
  583. } else if ((value & 0x0f) != pfuze_chip->chip_id &&
  584. (value & 0xf0) >> 4 != pfuze_chip->chip_id &&
  585. (value != pfuze_chip->chip_id)) {
  586. /* device id NOT match with your setting */
  587. dev_warn(pfuze_chip->dev, "Illegal ID: %x\n", value);
  588. return -ENODEV;
  589. }
  590. ret = regmap_read(pfuze_chip->regmap, PFUZE100_REVID, &value);
  591. if (ret)
  592. return ret;
  593. dev_info(pfuze_chip->dev,
  594. "Full layer: %x, Metal layer: %x\n",
  595. (value & 0xf0) >> 4, value & 0x0f);
  596. ret = regmap_read(pfuze_chip->regmap, PFUZE100_FABID, &value);
  597. if (ret)
  598. return ret;
  599. dev_info(pfuze_chip->dev, "FAB: %x, FIN: %x\n",
  600. (value & 0xc) >> 2, value & 0x3);
  601. return 0;
  602. }
  603. static const struct regmap_config pfuze_regmap_config = {
  604. .reg_bits = 8,
  605. .val_bits = 8,
  606. .max_register = PFUZE_NUMREGS - 1,
  607. .cache_type = REGCACHE_RBTREE,
  608. };
  609. static int pfuze100_regulator_probe(struct i2c_client *client,
  610. const struct i2c_device_id *id)
  611. {
  612. struct pfuze_chip *pfuze_chip;
  613. struct regulator_config config = { };
  614. int i, ret;
  615. const struct of_device_id *match;
  616. u32 regulator_num;
  617. u32 sw_check_start, sw_check_end, sw_hi = 0x40;
  618. pfuze_chip = devm_kzalloc(&client->dev, sizeof(*pfuze_chip),
  619. GFP_KERNEL);
  620. if (!pfuze_chip)
  621. return -ENOMEM;
  622. if (client->dev.of_node) {
  623. match = of_match_device(of_match_ptr(pfuze_dt_ids),
  624. &client->dev);
  625. if (!match) {
  626. dev_err(&client->dev, "Error: No device match found\n");
  627. return -ENODEV;
  628. }
  629. pfuze_chip->chip_id = (int)(long)match->data;
  630. } else if (id) {
  631. pfuze_chip->chip_id = id->driver_data;
  632. } else {
  633. dev_err(&client->dev, "No dts match or id table match found\n");
  634. return -ENODEV;
  635. }
  636. i2c_set_clientdata(client, pfuze_chip);
  637. pfuze_chip->dev = &client->dev;
  638. pfuze_chip->regmap = devm_regmap_init_i2c(client, &pfuze_regmap_config);
  639. if (IS_ERR(pfuze_chip->regmap)) {
  640. ret = PTR_ERR(pfuze_chip->regmap);
  641. dev_err(&client->dev,
  642. "regmap allocation failed with err %d\n", ret);
  643. return ret;
  644. }
  645. ret = pfuze_identify(pfuze_chip);
  646. if (ret) {
  647. dev_err(&client->dev, "unrecognized pfuze chip ID!\n");
  648. return ret;
  649. }
  650. /* use the right regulators after identify the right device */
  651. switch (pfuze_chip->chip_id) {
  652. case PFUZE3001:
  653. pfuze_chip->pfuze_regulators = pfuze3001_regulators;
  654. regulator_num = ARRAY_SIZE(pfuze3001_regulators);
  655. sw_check_start = PFUZE3001_SW2;
  656. sw_check_end = PFUZE3001_SW2;
  657. sw_hi = 1 << 3;
  658. break;
  659. case PFUZE3000:
  660. pfuze_chip->pfuze_regulators = pfuze3000_regulators;
  661. regulator_num = ARRAY_SIZE(pfuze3000_regulators);
  662. sw_check_start = PFUZE3000_SW2;
  663. sw_check_end = PFUZE3000_SW2;
  664. sw_hi = 1 << 3;
  665. break;
  666. case PFUZE200:
  667. pfuze_chip->pfuze_regulators = pfuze200_regulators;
  668. regulator_num = ARRAY_SIZE(pfuze200_regulators);
  669. sw_check_start = PFUZE200_SW2;
  670. sw_check_end = PFUZE200_SW3B;
  671. break;
  672. case PFUZE100:
  673. default:
  674. pfuze_chip->pfuze_regulators = pfuze100_regulators;
  675. regulator_num = ARRAY_SIZE(pfuze100_regulators);
  676. sw_check_start = PFUZE100_SW2;
  677. sw_check_end = PFUZE100_SW4;
  678. break;
  679. }
  680. dev_info(&client->dev, "pfuze%s found.\n",
  681. (pfuze_chip->chip_id == PFUZE100) ? "100" :
  682. (((pfuze_chip->chip_id == PFUZE200) ? "200" :
  683. ((pfuze_chip->chip_id == PFUZE3000) ? "3000" : "3001"))));
  684. memcpy(pfuze_chip->regulator_descs, pfuze_chip->pfuze_regulators,
  685. regulator_num * sizeof(struct pfuze_regulator));
  686. ret = pfuze_parse_regulators_dt(pfuze_chip);
  687. if (ret)
  688. return ret;
  689. for (i = 0; i < regulator_num; i++) {
  690. struct regulator_init_data *init_data;
  691. struct regulator_desc *desc;
  692. int val;
  693. desc = &pfuze_chip->regulator_descs[i].desc;
  694. init_data = match_init_data(i);
  695. /* SW2~SW4 high bit check and modify the voltage value table */
  696. if (i >= sw_check_start && i <= sw_check_end) {
  697. ret = regmap_read(pfuze_chip->regmap,
  698. desc->vsel_reg, &val);
  699. if (ret) {
  700. dev_err(&client->dev, "Fails to read from the register.\n");
  701. return ret;
  702. }
  703. if (val & sw_hi) {
  704. if (pfuze_chip->chip_id == PFUZE3000 ||
  705. pfuze_chip->chip_id == PFUZE3001) {
  706. desc->volt_table = pfuze3000_sw2hi;
  707. desc->n_voltages = ARRAY_SIZE(pfuze3000_sw2hi);
  708. } else {
  709. desc->min_uV = 800000;
  710. desc->uV_step = 50000;
  711. desc->n_voltages = 51;
  712. }
  713. }
  714. }
  715. /*
  716. * Allow SW regulators to turn off. Checking it trough a flag is
  717. * a workaround to keep the backward compatibility with existing
  718. * old dtb's which may relay on the fact that we didn't disable
  719. * the switched regulator till yet.
  720. */
  721. if (pfuze_chip->flags & PFUZE_FLAG_DISABLE_SW) {
  722. if (pfuze_chip->chip_id == PFUZE100 ||
  723. pfuze_chip->chip_id == PFUZE200) {
  724. if (pfuze_chip->regulator_descs[i].sw_reg) {
  725. desc->ops = &pfuze100_sw_disable_regulator_ops;
  726. desc->enable_val = 0x8;
  727. desc->disable_val = 0x0;
  728. desc->enable_time = 500;
  729. }
  730. }
  731. }
  732. config.dev = &client->dev;
  733. config.init_data = init_data;
  734. config.driver_data = pfuze_chip;
  735. config.of_node = match_of_node(i);
  736. pfuze_chip->regulators[i] =
  737. devm_regulator_register(&client->dev, desc, &config);
  738. if (IS_ERR(pfuze_chip->regulators[i])) {
  739. dev_err(&client->dev, "register regulator%s failed\n",
  740. pfuze_chip->pfuze_regulators[i].desc.name);
  741. return PTR_ERR(pfuze_chip->regulators[i]);
  742. }
  743. }
  744. if (of_property_read_bool(client->dev.of_node,
  745. "fsl,pmic-stby-poweroff"))
  746. return pfuze_power_off_prepare_init(pfuze_chip);
  747. return 0;
  748. }
  749. static struct i2c_driver pfuze_driver = {
  750. .driver = {
  751. .name = "pfuze100-regulator",
  752. .of_match_table = pfuze_dt_ids,
  753. },
  754. .probe = pfuze100_regulator_probe,
  755. };
  756. module_i2c_driver(pfuze_driver);
  757. MODULE_AUTHOR("Robin Gong <[email protected]>");
  758. MODULE_DESCRIPTION("Regulator Driver for Freescale PFUZE100/200/3000/3001 PMIC");
  759. MODULE_LICENSE("GPL v2");