pfuze100-regulator.c 26 KB

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