pfuze100-regulator.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. static const struct regulator_ops pfuze3000_sw_regulator_ops = {
  184. .enable = regulator_enable_regmap,
  185. .disable = regulator_disable_regmap,
  186. .is_enabled = regulator_is_enabled_regmap,
  187. .list_voltage = regulator_list_voltage_table,
  188. .map_voltage = regulator_map_voltage_ascend,
  189. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  190. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  191. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  192. .set_ramp_delay = pfuze100_set_ramp_delay,
  193. };
  194. #define PFUZE100_FIXED_REG(_chip, _name, base, voltage) \
  195. [_chip ## _ ## _name] = { \
  196. .desc = { \
  197. .name = #_name, \
  198. .n_voltages = 1, \
  199. .ops = &pfuze100_fixed_regulator_ops, \
  200. .type = REGULATOR_VOLTAGE, \
  201. .id = _chip ## _ ## _name, \
  202. .owner = THIS_MODULE, \
  203. .min_uV = (voltage), \
  204. .enable_reg = (base), \
  205. .enable_mask = 0x10, \
  206. }, \
  207. }
  208. #define PFUZE100_SW_REG(_chip, _name, base, min, max, step) \
  209. [_chip ## _ ## _name] = { \
  210. .desc = { \
  211. .name = #_name,\
  212. .n_voltages = ((max) - (min)) / (step) + 1, \
  213. .ops = &pfuze100_sw_regulator_ops, \
  214. .type = REGULATOR_VOLTAGE, \
  215. .id = _chip ## _ ## _name, \
  216. .owner = THIS_MODULE, \
  217. .min_uV = (min), \
  218. .uV_step = (step), \
  219. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  220. .vsel_mask = 0x3f, \
  221. .enable_reg = (base) + PFUZE100_MODE_OFFSET, \
  222. .enable_mask = 0xf, \
  223. }, \
  224. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  225. .stby_mask = 0x3f, \
  226. .sw_reg = true, \
  227. }
  228. #define PFUZE100_SWB_REG(_chip, _name, base, mask, voltages) \
  229. [_chip ## _ ## _name] = { \
  230. .desc = { \
  231. .name = #_name, \
  232. .n_voltages = ARRAY_SIZE(voltages), \
  233. .ops = &pfuze100_swb_regulator_ops, \
  234. .type = REGULATOR_VOLTAGE, \
  235. .id = _chip ## _ ## _name, \
  236. .owner = THIS_MODULE, \
  237. .volt_table = voltages, \
  238. .vsel_reg = (base), \
  239. .vsel_mask = (mask), \
  240. .enable_reg = (base), \
  241. .enable_mask = 0x48, \
  242. }, \
  243. }
  244. #define PFUZE100_VGEN_REG(_chip, _name, base, min, max, step) \
  245. [_chip ## _ ## _name] = { \
  246. .desc = { \
  247. .name = #_name, \
  248. .n_voltages = ((max) - (min)) / (step) + 1, \
  249. .ops = &pfuze100_ldo_regulator_ops, \
  250. .type = REGULATOR_VOLTAGE, \
  251. .id = _chip ## _ ## _name, \
  252. .owner = THIS_MODULE, \
  253. .min_uV = (min), \
  254. .uV_step = (step), \
  255. .vsel_reg = (base), \
  256. .vsel_mask = 0xf, \
  257. .enable_reg = (base), \
  258. .enable_mask = 0x10, \
  259. }, \
  260. .stby_reg = (base), \
  261. .stby_mask = 0x20, \
  262. }
  263. #define PFUZE100_COIN_REG(_chip, _name, base, mask, voltages) \
  264. [_chip ## _ ## _name] = { \
  265. .desc = { \
  266. .name = #_name, \
  267. .n_voltages = ARRAY_SIZE(voltages), \
  268. .ops = &pfuze100_swb_regulator_ops, \
  269. .type = REGULATOR_VOLTAGE, \
  270. .id = _chip ## _ ## _name, \
  271. .owner = THIS_MODULE, \
  272. .volt_table = voltages, \
  273. .vsel_reg = (base), \
  274. .vsel_mask = (mask), \
  275. .enable_reg = (base), \
  276. .enable_mask = 0x8, \
  277. }, \
  278. }
  279. #define PFUZE3000_VCC_REG(_chip, _name, base, min, max, step) { \
  280. .desc = { \
  281. .name = #_name, \
  282. .n_voltages = ((max) - (min)) / (step) + 1, \
  283. .ops = &pfuze100_ldo_regulator_ops, \
  284. .type = REGULATOR_VOLTAGE, \
  285. .id = _chip ## _ ## _name, \
  286. .owner = THIS_MODULE, \
  287. .min_uV = (min), \
  288. .uV_step = (step), \
  289. .vsel_reg = (base), \
  290. .vsel_mask = 0x3, \
  291. .enable_reg = (base), \
  292. .enable_mask = 0x10, \
  293. }, \
  294. .stby_reg = (base), \
  295. .stby_mask = 0x20, \
  296. }
  297. /* No linar case for the some switches of PFUZE3000 */
  298. #define PFUZE3000_SW_REG(_chip, _name, base, mask, voltages) \
  299. [_chip ## _ ## _name] = { \
  300. .desc = { \
  301. .name = #_name, \
  302. .n_voltages = ARRAY_SIZE(voltages), \
  303. .ops = &pfuze3000_sw_regulator_ops, \
  304. .type = REGULATOR_VOLTAGE, \
  305. .id = _chip ## _ ## _name, \
  306. .owner = THIS_MODULE, \
  307. .volt_table = voltages, \
  308. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  309. .vsel_mask = (mask), \
  310. .enable_reg = (base) + PFUZE100_MODE_OFFSET, \
  311. .enable_mask = 0xf, \
  312. .enable_val = 0x8, \
  313. .enable_time = 500, \
  314. }, \
  315. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  316. .stby_mask = (mask), \
  317. .sw_reg = true, \
  318. }
  319. #define PFUZE3000_SW3_REG(_chip, _name, base, min, max, step) { \
  320. .desc = { \
  321. .name = #_name,\
  322. .n_voltages = ((max) - (min)) / (step) + 1, \
  323. .ops = &pfuze100_sw_regulator_ops, \
  324. .type = REGULATOR_VOLTAGE, \
  325. .id = _chip ## _ ## _name, \
  326. .owner = THIS_MODULE, \
  327. .min_uV = (min), \
  328. .uV_step = (step), \
  329. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  330. .vsel_mask = 0xf, \
  331. }, \
  332. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  333. .stby_mask = 0xf, \
  334. }
  335. /* PFUZE100 */
  336. static struct pfuze_regulator pfuze100_regulators[] = {
  337. PFUZE100_SW_REG(PFUZE100, SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  338. PFUZE100_SW_REG(PFUZE100, SW1C, PFUZE100_SW1CVOL, 300000, 1875000, 25000),
  339. PFUZE100_SW_REG(PFUZE100, SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  340. PFUZE100_SW_REG(PFUZE100, SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  341. PFUZE100_SW_REG(PFUZE100, SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  342. PFUZE100_SW_REG(PFUZE100, SW4, PFUZE100_SW4VOL, 400000, 1975000, 25000),
  343. PFUZE100_SWB_REG(PFUZE100, SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  344. PFUZE100_SWB_REG(PFUZE100, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  345. PFUZE100_FIXED_REG(PFUZE100, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  346. PFUZE100_VGEN_REG(PFUZE100, VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  347. PFUZE100_VGEN_REG(PFUZE100, VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  348. PFUZE100_VGEN_REG(PFUZE100, VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  349. PFUZE100_VGEN_REG(PFUZE100, VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  350. PFUZE100_VGEN_REG(PFUZE100, VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  351. PFUZE100_VGEN_REG(PFUZE100, VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  352. PFUZE100_COIN_REG(PFUZE100, COIN, PFUZE100_COINVOL, 0x7, pfuze100_coin),
  353. };
  354. static struct pfuze_regulator pfuze200_regulators[] = {
  355. PFUZE100_SW_REG(PFUZE200, SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  356. PFUZE100_SW_REG(PFUZE200, SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  357. PFUZE100_SW_REG(PFUZE200, SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  358. PFUZE100_SW_REG(PFUZE200, SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  359. PFUZE100_SWB_REG(PFUZE200, SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  360. PFUZE100_SWB_REG(PFUZE200, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  361. PFUZE100_FIXED_REG(PFUZE200, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  362. PFUZE100_VGEN_REG(PFUZE200, VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  363. PFUZE100_VGEN_REG(PFUZE200, VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  364. PFUZE100_VGEN_REG(PFUZE200, VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  365. PFUZE100_VGEN_REG(PFUZE200, VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  366. PFUZE100_VGEN_REG(PFUZE200, VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  367. PFUZE100_VGEN_REG(PFUZE200, VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  368. PFUZE100_COIN_REG(PFUZE200, COIN, PFUZE100_COINVOL, 0x7, pfuze100_coin),
  369. };
  370. static struct pfuze_regulator pfuze3000_regulators[] = {
  371. PFUZE3000_SW_REG(PFUZE3000, SW1A, PFUZE100_SW1ABVOL, 0x1f, pfuze3000_sw1a),
  372. PFUZE100_SW_REG(PFUZE3000, SW1B, PFUZE100_SW1CVOL, 700000, 1475000, 25000),
  373. PFUZE3000_SW_REG(PFUZE3000, SW2, PFUZE100_SW2VOL, 0x7, pfuze3000_sw2lo),
  374. PFUZE3000_SW3_REG(PFUZE3000, SW3, PFUZE100_SW3AVOL, 900000, 1650000, 50000),
  375. PFUZE100_SWB_REG(PFUZE3000, SWBST, PFUZE100_SWBSTCON1, 0x3, pfuze100_swbst),
  376. PFUZE100_SWB_REG(PFUZE3000, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  377. PFUZE100_FIXED_REG(PFUZE3000, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  378. PFUZE100_VGEN_REG(PFUZE3000, VLDO1, PFUZE100_VGEN1VOL, 1800000, 3300000, 100000),
  379. PFUZE100_VGEN_REG(PFUZE3000, VLDO2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  380. PFUZE3000_VCC_REG(PFUZE3000, VCCSD, PFUZE100_VGEN3VOL, 2850000, 3300000, 150000),
  381. PFUZE3000_VCC_REG(PFUZE3000, V33, PFUZE100_VGEN4VOL, 2850000, 3300000, 150000),
  382. PFUZE100_VGEN_REG(PFUZE3000, VLDO3, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  383. PFUZE100_VGEN_REG(PFUZE3000, VLDO4, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  384. };
  385. static struct pfuze_regulator pfuze3001_regulators[] = {
  386. PFUZE3000_SW_REG(PFUZE3001, SW1, PFUZE100_SW1ABVOL, 0x1f, pfuze3000_sw1a),
  387. PFUZE3000_SW_REG(PFUZE3001, SW2, PFUZE100_SW2VOL, 0x7, pfuze3000_sw2lo),
  388. PFUZE3000_SW3_REG(PFUZE3001, SW3, PFUZE100_SW3AVOL, 900000, 1650000, 50000),
  389. PFUZE100_SWB_REG(PFUZE3001, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  390. PFUZE100_VGEN_REG(PFUZE3001, VLDO1, PFUZE100_VGEN1VOL, 1800000, 3300000, 100000),
  391. PFUZE100_VGEN_REG(PFUZE3001, VLDO2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  392. PFUZE3000_VCC_REG(PFUZE3001, VCCSD, PFUZE100_VGEN3VOL, 2850000, 3300000, 150000),
  393. PFUZE3000_VCC_REG(PFUZE3001, V33, PFUZE100_VGEN4VOL, 2850000, 3300000, 150000),
  394. PFUZE100_VGEN_REG(PFUZE3001, VLDO3, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  395. PFUZE100_VGEN_REG(PFUZE3001, VLDO4, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  396. };
  397. #ifdef CONFIG_OF
  398. /* PFUZE100 */
  399. static struct of_regulator_match pfuze100_matches[] = {
  400. { .name = "sw1ab", },
  401. { .name = "sw1c", },
  402. { .name = "sw2", },
  403. { .name = "sw3a", },
  404. { .name = "sw3b", },
  405. { .name = "sw4", },
  406. { .name = "swbst", },
  407. { .name = "vsnvs", },
  408. { .name = "vrefddr", },
  409. { .name = "vgen1", },
  410. { .name = "vgen2", },
  411. { .name = "vgen3", },
  412. { .name = "vgen4", },
  413. { .name = "vgen5", },
  414. { .name = "vgen6", },
  415. { .name = "coin", },
  416. };
  417. /* PFUZE200 */
  418. static struct of_regulator_match pfuze200_matches[] = {
  419. { .name = "sw1ab", },
  420. { .name = "sw2", },
  421. { .name = "sw3a", },
  422. { .name = "sw3b", },
  423. { .name = "swbst", },
  424. { .name = "vsnvs", },
  425. { .name = "vrefddr", },
  426. { .name = "vgen1", },
  427. { .name = "vgen2", },
  428. { .name = "vgen3", },
  429. { .name = "vgen4", },
  430. { .name = "vgen5", },
  431. { .name = "vgen6", },
  432. { .name = "coin", },
  433. };
  434. /* PFUZE3000 */
  435. static struct of_regulator_match pfuze3000_matches[] = {
  436. { .name = "sw1a", },
  437. { .name = "sw1b", },
  438. { .name = "sw2", },
  439. { .name = "sw3", },
  440. { .name = "swbst", },
  441. { .name = "vsnvs", },
  442. { .name = "vrefddr", },
  443. { .name = "vldo1", },
  444. { .name = "vldo2", },
  445. { .name = "vccsd", },
  446. { .name = "v33", },
  447. { .name = "vldo3", },
  448. { .name = "vldo4", },
  449. };
  450. /* PFUZE3001 */
  451. static struct of_regulator_match pfuze3001_matches[] = {
  452. { .name = "sw1", },
  453. { .name = "sw2", },
  454. { .name = "sw3", },
  455. { .name = "vsnvs", },
  456. { .name = "vldo1", },
  457. { .name = "vldo2", },
  458. { .name = "vccsd", },
  459. { .name = "v33", },
  460. { .name = "vldo3", },
  461. { .name = "vldo4", },
  462. };
  463. static struct of_regulator_match *pfuze_matches;
  464. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  465. {
  466. struct device *dev = chip->dev;
  467. struct device_node *np, *parent;
  468. int ret;
  469. np = of_node_get(dev->of_node);
  470. if (!np)
  471. return -EINVAL;
  472. if (of_property_read_bool(np, "fsl,pfuze-support-disable-sw"))
  473. chip->flags |= PFUZE_FLAG_DISABLE_SW;
  474. parent = of_get_child_by_name(np, "regulators");
  475. if (!parent) {
  476. dev_err(dev, "regulators node not found\n");
  477. return -EINVAL;
  478. }
  479. switch (chip->chip_id) {
  480. case PFUZE3001:
  481. pfuze_matches = pfuze3001_matches;
  482. ret = of_regulator_match(dev, parent, pfuze3001_matches,
  483. ARRAY_SIZE(pfuze3001_matches));
  484. break;
  485. case PFUZE3000:
  486. pfuze_matches = pfuze3000_matches;
  487. ret = of_regulator_match(dev, parent, pfuze3000_matches,
  488. ARRAY_SIZE(pfuze3000_matches));
  489. break;
  490. case PFUZE200:
  491. pfuze_matches = pfuze200_matches;
  492. ret = of_regulator_match(dev, parent, pfuze200_matches,
  493. ARRAY_SIZE(pfuze200_matches));
  494. break;
  495. case PFUZE100:
  496. default:
  497. pfuze_matches = pfuze100_matches;
  498. ret = of_regulator_match(dev, parent, pfuze100_matches,
  499. ARRAY_SIZE(pfuze100_matches));
  500. break;
  501. }
  502. of_node_put(parent);
  503. if (ret < 0) {
  504. dev_err(dev, "Error parsing regulator init data: %d\n",
  505. ret);
  506. return ret;
  507. }
  508. return 0;
  509. }
  510. static inline struct regulator_init_data *match_init_data(int index)
  511. {
  512. return pfuze_matches[index].init_data;
  513. }
  514. static inline struct device_node *match_of_node(int index)
  515. {
  516. return pfuze_matches[index].of_node;
  517. }
  518. #else
  519. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  520. {
  521. return 0;
  522. }
  523. static inline struct regulator_init_data *match_init_data(int index)
  524. {
  525. return NULL;
  526. }
  527. static inline struct device_node *match_of_node(int index)
  528. {
  529. return NULL;
  530. }
  531. #endif
  532. static struct pfuze_chip *syspm_pfuze_chip;
  533. static void pfuze_power_off_prepare(void)
  534. {
  535. dev_info(syspm_pfuze_chip->dev, "Configure standby mode for power off");
  536. /* Switch from default mode: APS/APS to APS/Off */
  537. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW1ABMODE,
  538. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  539. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW1CMODE,
  540. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  541. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW2MODE,
  542. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  543. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW3AMODE,
  544. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  545. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW3BMODE,
  546. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  547. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_SW4MODE,
  548. PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
  549. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN1VOL,
  550. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  551. PFUZE100_VGENxSTBY);
  552. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN2VOL,
  553. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  554. PFUZE100_VGENxSTBY);
  555. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN3VOL,
  556. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  557. PFUZE100_VGENxSTBY);
  558. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN4VOL,
  559. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  560. PFUZE100_VGENxSTBY);
  561. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN5VOL,
  562. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  563. PFUZE100_VGENxSTBY);
  564. regmap_update_bits(syspm_pfuze_chip->regmap, PFUZE100_VGEN6VOL,
  565. PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
  566. PFUZE100_VGENxSTBY);
  567. }
  568. static int pfuze_power_off_prepare_init(struct pfuze_chip *pfuze_chip)
  569. {
  570. if (pfuze_chip->chip_id != PFUZE100) {
  571. dev_warn(pfuze_chip->dev, "Requested pm_power_off_prepare handler for not supported chip\n");
  572. return -ENODEV;
  573. }
  574. if (pm_power_off_prepare) {
  575. dev_warn(pfuze_chip->dev, "pm_power_off_prepare is already registered.\n");
  576. return -EBUSY;
  577. }
  578. if (syspm_pfuze_chip) {
  579. dev_warn(pfuze_chip->dev, "syspm_pfuze_chip is already set.\n");
  580. return -EBUSY;
  581. }
  582. syspm_pfuze_chip = pfuze_chip;
  583. pm_power_off_prepare = pfuze_power_off_prepare;
  584. return 0;
  585. }
  586. static int pfuze_identify(struct pfuze_chip *pfuze_chip)
  587. {
  588. unsigned int value;
  589. int ret;
  590. ret = regmap_read(pfuze_chip->regmap, PFUZE100_DEVICEID, &value);
  591. if (ret)
  592. return ret;
  593. if (((value & 0x0f) == 0x8) && (pfuze_chip->chip_id == PFUZE100)) {
  594. /*
  595. * Freescale misprogrammed 1-3% of parts prior to week 8 of 2013
  596. * as ID=8 in PFUZE100
  597. */
  598. dev_info(pfuze_chip->dev, "Assuming misprogrammed ID=0x8");
  599. } else if ((value & 0x0f) != pfuze_chip->chip_id &&
  600. (value & 0xf0) >> 4 != pfuze_chip->chip_id &&
  601. (value != pfuze_chip->chip_id)) {
  602. /* device id NOT match with your setting */
  603. dev_warn(pfuze_chip->dev, "Illegal ID: %x\n", value);
  604. return -ENODEV;
  605. }
  606. ret = regmap_read(pfuze_chip->regmap, PFUZE100_REVID, &value);
  607. if (ret)
  608. return ret;
  609. dev_info(pfuze_chip->dev,
  610. "Full layer: %x, Metal layer: %x\n",
  611. (value & 0xf0) >> 4, value & 0x0f);
  612. ret = regmap_read(pfuze_chip->regmap, PFUZE100_FABID, &value);
  613. if (ret)
  614. return ret;
  615. dev_info(pfuze_chip->dev, "FAB: %x, FIN: %x\n",
  616. (value & 0xc) >> 2, value & 0x3);
  617. return 0;
  618. }
  619. static const struct regmap_config pfuze_regmap_config = {
  620. .reg_bits = 8,
  621. .val_bits = 8,
  622. .max_register = PFUZE_NUMREGS - 1,
  623. .cache_type = REGCACHE_RBTREE,
  624. };
  625. static int pfuze100_regulator_probe(struct i2c_client *client,
  626. const struct i2c_device_id *id)
  627. {
  628. struct pfuze_chip *pfuze_chip;
  629. struct pfuze_regulator_platform_data *pdata =
  630. dev_get_platdata(&client->dev);
  631. struct regulator_config config = { };
  632. int i, ret;
  633. const struct of_device_id *match;
  634. u32 regulator_num;
  635. u32 sw_check_start, sw_check_end, sw_hi = 0x40;
  636. pfuze_chip = devm_kzalloc(&client->dev, sizeof(*pfuze_chip),
  637. GFP_KERNEL);
  638. if (!pfuze_chip)
  639. return -ENOMEM;
  640. if (client->dev.of_node) {
  641. match = of_match_device(of_match_ptr(pfuze_dt_ids),
  642. &client->dev);
  643. if (!match) {
  644. dev_err(&client->dev, "Error: No device match found\n");
  645. return -ENODEV;
  646. }
  647. pfuze_chip->chip_id = (int)(long)match->data;
  648. } else if (id) {
  649. pfuze_chip->chip_id = id->driver_data;
  650. } else {
  651. dev_err(&client->dev, "No dts match or id table match found\n");
  652. return -ENODEV;
  653. }
  654. i2c_set_clientdata(client, pfuze_chip);
  655. pfuze_chip->dev = &client->dev;
  656. pfuze_chip->regmap = devm_regmap_init_i2c(client, &pfuze_regmap_config);
  657. if (IS_ERR(pfuze_chip->regmap)) {
  658. ret = PTR_ERR(pfuze_chip->regmap);
  659. dev_err(&client->dev,
  660. "regmap allocation failed with err %d\n", ret);
  661. return ret;
  662. }
  663. ret = pfuze_identify(pfuze_chip);
  664. if (ret) {
  665. dev_err(&client->dev, "unrecognized pfuze chip ID!\n");
  666. return ret;
  667. }
  668. /* use the right regulators after identify the right device */
  669. switch (pfuze_chip->chip_id) {
  670. case PFUZE3001:
  671. pfuze_chip->pfuze_regulators = pfuze3001_regulators;
  672. regulator_num = ARRAY_SIZE(pfuze3001_regulators);
  673. sw_check_start = PFUZE3001_SW2;
  674. sw_check_end = PFUZE3001_SW2;
  675. sw_hi = 1 << 3;
  676. break;
  677. case PFUZE3000:
  678. pfuze_chip->pfuze_regulators = pfuze3000_regulators;
  679. regulator_num = ARRAY_SIZE(pfuze3000_regulators);
  680. sw_check_start = PFUZE3000_SW2;
  681. sw_check_end = PFUZE3000_SW2;
  682. sw_hi = 1 << 3;
  683. break;
  684. case PFUZE200:
  685. pfuze_chip->pfuze_regulators = pfuze200_regulators;
  686. regulator_num = ARRAY_SIZE(pfuze200_regulators);
  687. sw_check_start = PFUZE200_SW2;
  688. sw_check_end = PFUZE200_SW3B;
  689. break;
  690. case PFUZE100:
  691. default:
  692. pfuze_chip->pfuze_regulators = pfuze100_regulators;
  693. regulator_num = ARRAY_SIZE(pfuze100_regulators);
  694. sw_check_start = PFUZE100_SW2;
  695. sw_check_end = PFUZE100_SW4;
  696. break;
  697. }
  698. dev_info(&client->dev, "pfuze%s found.\n",
  699. (pfuze_chip->chip_id == PFUZE100) ? "100" :
  700. (((pfuze_chip->chip_id == PFUZE200) ? "200" :
  701. ((pfuze_chip->chip_id == PFUZE3000) ? "3000" : "3001"))));
  702. memcpy(pfuze_chip->regulator_descs, pfuze_chip->pfuze_regulators,
  703. sizeof(pfuze_chip->regulator_descs));
  704. ret = pfuze_parse_regulators_dt(pfuze_chip);
  705. if (ret)
  706. return ret;
  707. for (i = 0; i < regulator_num; i++) {
  708. struct regulator_init_data *init_data;
  709. struct regulator_desc *desc;
  710. int val;
  711. desc = &pfuze_chip->regulator_descs[i].desc;
  712. if (pdata)
  713. init_data = pdata->init_data[i];
  714. else
  715. init_data = match_init_data(i);
  716. /* SW2~SW4 high bit check and modify the voltage value table */
  717. if (i >= sw_check_start && i <= sw_check_end) {
  718. ret = regmap_read(pfuze_chip->regmap,
  719. desc->vsel_reg, &val);
  720. if (ret) {
  721. dev_err(&client->dev, "Fails to read from the register.\n");
  722. return ret;
  723. }
  724. if (val & sw_hi) {
  725. if (pfuze_chip->chip_id == PFUZE3000 ||
  726. pfuze_chip->chip_id == PFUZE3001) {
  727. desc->volt_table = pfuze3000_sw2hi;
  728. desc->n_voltages = ARRAY_SIZE(pfuze3000_sw2hi);
  729. } else {
  730. desc->min_uV = 800000;
  731. desc->uV_step = 50000;
  732. desc->n_voltages = 51;
  733. }
  734. }
  735. }
  736. /*
  737. * Allow SW regulators to turn off. Checking it trough a flag is
  738. * a workaround to keep the backward compatibility with existing
  739. * old dtb's which may relay on the fact that we didn't disable
  740. * the switched regulator till yet.
  741. */
  742. if (pfuze_chip->flags & PFUZE_FLAG_DISABLE_SW) {
  743. if (pfuze_chip->chip_id == PFUZE100 ||
  744. pfuze_chip->chip_id == PFUZE200) {
  745. if (pfuze_chip->regulator_descs[i].sw_reg) {
  746. desc->ops = &pfuze100_sw_disable_regulator_ops;
  747. desc->enable_val = 0x8;
  748. desc->disable_val = 0x0;
  749. desc->enable_time = 500;
  750. }
  751. }
  752. }
  753. config.dev = &client->dev;
  754. config.init_data = init_data;
  755. config.driver_data = pfuze_chip;
  756. config.of_node = match_of_node(i);
  757. pfuze_chip->regulators[i] =
  758. devm_regulator_register(&client->dev, desc, &config);
  759. if (IS_ERR(pfuze_chip->regulators[i])) {
  760. dev_err(&client->dev, "register regulator%s failed\n",
  761. pfuze_chip->pfuze_regulators[i].desc.name);
  762. return PTR_ERR(pfuze_chip->regulators[i]);
  763. }
  764. }
  765. if (of_property_read_bool(client->dev.of_node,
  766. "fsl,pmic-stby-poweroff"))
  767. return pfuze_power_off_prepare_init(pfuze_chip);
  768. return 0;
  769. }
  770. static int pfuze100_regulator_remove(struct i2c_client *client)
  771. {
  772. if (syspm_pfuze_chip) {
  773. syspm_pfuze_chip = NULL;
  774. pm_power_off_prepare = NULL;
  775. }
  776. return 0;
  777. }
  778. static struct i2c_driver pfuze_driver = {
  779. .id_table = pfuze_device_id,
  780. .driver = {
  781. .name = "pfuze100-regulator",
  782. .of_match_table = pfuze_dt_ids,
  783. },
  784. .probe = pfuze100_regulator_probe,
  785. .remove = pfuze100_regulator_remove,
  786. };
  787. module_i2c_driver(pfuze_driver);
  788. MODULE_AUTHOR("Robin Gong <b38343@freescale.com>");
  789. MODULE_DESCRIPTION("Regulator Driver for Freescale PFUZE100/200/3000/3001 PMIC");
  790. MODULE_LICENSE("GPL v2");