pm8xxx-misc.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /*
  2. * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/delay.h>
  21. #include <linux/mfd/pm8xxx/core.h>
  22. #include <linux/mfd/pm8xxx/misc.h>
  23. /* PON CTRL 1 register */
  24. #define REG_PM8XXX_PON_CTRL_1 0x01C
  25. #define PON_CTRL_1_PULL_UP_MASK 0xE0
  26. #define PON_CTRL_1_USB_PWR_EN 0x10
  27. #define PON_CTRL_1_WD_EN_MASK 0x08
  28. #define PON_CTRL_1_WD_EN_RESET 0x08
  29. #define PON_CTRL_1_WD_EN_PWR_OFF 0x00
  30. /* PON CNTL registers */
  31. #define REG_PM8058_PON_CNTL_4 0x098
  32. #define REG_PM8901_PON_CNTL_4 0x099
  33. #define REG_PM8018_PON_CNTL_4 0x01E
  34. #define REG_PM8921_PON_CNTL_4 0x01E
  35. #define REG_PM8058_PON_CNTL_5 0x07B
  36. #define REG_PM8901_PON_CNTL_5 0x09A
  37. #define REG_PM8018_PON_CNTL_5 0x01F
  38. #define REG_PM8921_PON_CNTL_5 0x01F
  39. #define PON_CTRL_4_RESET_EN_MASK 0x01
  40. #define PON_CTRL_4_SHUTDOWN_ON_RESET 0x0
  41. #define PON_CTRL_4_RESTART_ON_RESET 0x1
  42. #define PON_CTRL_5_HARD_RESET_EN_MASK 0x08
  43. #define PON_CTRL_5_HARD_RESET_EN 0x08
  44. #define PON_CTRL_5_HARD_RESET_DIS 0x00
  45. /* Regulator master enable addresses */
  46. #define REG_PM8058_VREG_EN_MSM 0x018
  47. #define REG_PM8058_VREG_EN_GRP_5_4 0x1C8
  48. /* Regulator control registers for shutdown/reset */
  49. #define REG_PM8058_S0_CTRL 0x004
  50. #define REG_PM8058_S1_CTRL 0x005
  51. #define REG_PM8058_S3_CTRL 0x111
  52. #define REG_PM8058_L21_CTRL 0x120
  53. #define REG_PM8058_L22_CTRL 0x121
  54. #define PM8058_REGULATOR_ENABLE_MASK 0x80
  55. #define PM8058_REGULATOR_ENABLE 0x80
  56. #define PM8058_REGULATOR_DISABLE 0x00
  57. #define PM8058_REGULATOR_PULL_DOWN_MASK 0x40
  58. #define PM8058_REGULATOR_PULL_DOWN_EN 0x40
  59. /* Buck CTRL register */
  60. #define PM8058_SMPS_LEGACY_VREF_SEL 0x20
  61. #define PM8058_SMPS_LEGACY_VPROG_MASK 0x1F
  62. #define PM8058_SMPS_ADVANCED_BAND_MASK 0xC0
  63. #define PM8058_SMPS_ADVANCED_BAND_SHIFT 6
  64. #define PM8058_SMPS_ADVANCED_VPROG_MASK 0x3F
  65. /* Buck TEST2 registers for shutdown/reset */
  66. #define REG_PM8058_S0_TEST2 0x084
  67. #define REG_PM8058_S1_TEST2 0x085
  68. #define REG_PM8058_S3_TEST2 0x11A
  69. #define PM8058_REGULATOR_BANK_WRITE 0x80
  70. #define PM8058_REGULATOR_BANK_MASK 0x70
  71. #define PM8058_REGULATOR_BANK_SHIFT 4
  72. #define PM8058_REGULATOR_BANK_SEL(n) ((n) << PM8058_REGULATOR_BANK_SHIFT)
  73. /* Buck TEST2 register bank 1 */
  74. #define PM8058_SMPS_LEGACY_VLOW_SEL 0x01
  75. /* Buck TEST2 register bank 7 */
  76. #define PM8058_SMPS_ADVANCED_MODE_MASK 0x02
  77. #define PM8058_SMPS_ADVANCED_MODE 0x02
  78. #define PM8058_SMPS_LEGACY_MODE 0x00
  79. /* SLEEP CTRL register */
  80. #define REG_PM8058_SLEEP_CTRL 0x02B
  81. #define REG_PM8921_SLEEP_CTRL 0x10A
  82. #define REG_PM8018_SLEEP_CTRL 0x10A
  83. #define SLEEP_CTRL_SMPL_EN_MASK 0x04
  84. #define SLEEP_CTRL_SMPL_EN_RESET 0x04
  85. #define SLEEP_CTRL_SMPL_EN_PWR_OFF 0x00
  86. #define SLEEP_CTRL_SMPL_SEL_MASK 0x03
  87. #define SLEEP_CTRL_SMPL_SEL_MIN 0
  88. #define SLEEP_CTRL_SMPL_SEL_MAX 3
  89. /* FTS regulator PMR registers */
  90. #define REG_PM8901_REGULATOR_S1_PMR 0xA7
  91. #define REG_PM8901_REGULATOR_S2_PMR 0xA8
  92. #define REG_PM8901_REGULATOR_S3_PMR 0xA9
  93. #define REG_PM8901_REGULATOR_S4_PMR 0xAA
  94. #define PM8901_REGULATOR_PMR_STATE_MASK 0x60
  95. #define PM8901_REGULATOR_PMR_STATE_OFF 0x20
  96. /* COINCELL CHG registers */
  97. #define REG_PM8058_COIN_CHG 0x02F
  98. #define REG_PM8921_COIN_CHG 0x09C
  99. #define REG_PM8018_COIN_CHG 0x09C
  100. #define COINCELL_RESISTOR_SHIFT 0x2
  101. /* GP TEST register */
  102. #define REG_PM8XXX_GP_TEST_1 0x07A
  103. /* Stay on configuration */
  104. #define PM8XXX_STAY_ON_CFG 0x92
  105. /* GPIO UART MUX CTRL registers */
  106. #define REG_PM8XXX_GPIO_MUX_CTRL 0x1CC
  107. #define UART_PATH_SEL_MASK 0x60
  108. #define UART_PATH_SEL_SHIFT 0x5
  109. #define USB_ID_PU_EN_MASK 0x10 /* PM8921 family only */
  110. #define USB_ID_PU_EN_SHIFT 4
  111. /* Shutdown/restart delays to allow for LDO 7/dVdd regulator load settling. */
  112. #define PM8901_DELAY_AFTER_REG_DISABLE_MS 4
  113. #define PM8901_DELAY_BEFORE_SHUTDOWN_MS 8
  114. #define REG_PM8XXX_XO_CNTRL_2 0x114
  115. #define MP3_1_MASK 0xE0
  116. #define MP3_2_MASK 0x1C
  117. #define MP3_1_SHIFT 5
  118. #define MP3_2_SHIFT 2
  119. #define REG_HSED_BIAS0_CNTL2 0xA1
  120. #define REG_HSED_BIAS1_CNTL2 0x135
  121. #define REG_HSED_BIAS2_CNTL2 0x138
  122. #define HSED_EN_MASK 0xC0
  123. struct pm8xxx_misc_chip {
  124. struct list_head link;
  125. struct pm8xxx_misc_platform_data pdata;
  126. struct device *dev;
  127. enum pm8xxx_version version;
  128. u64 osc_halt_count;
  129. };
  130. static LIST_HEAD(pm8xxx_misc_chips);
  131. static DEFINE_SPINLOCK(pm8xxx_misc_chips_lock);
  132. static int pm8xxx_misc_masked_write(struct pm8xxx_misc_chip *chip, u16 addr,
  133. u8 mask, u8 val)
  134. {
  135. int rc;
  136. u8 reg;
  137. rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
  138. if (rc) {
  139. pr_err("pm8xxx_readb(0x%03X) failed, rc=%d\n", addr, rc);
  140. return rc;
  141. }
  142. reg &= ~mask;
  143. reg |= val & mask;
  144. rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
  145. if (rc)
  146. pr_err("pm8xxx_writeb(0x%03X)=0x%02X failed, rc=%d\n", addr,
  147. reg, rc);
  148. return rc;
  149. }
  150. /**
  151. * pm8xxx_read_register - Read a PMIC register
  152. * @addr: PMIC register address
  153. * @value: Output parameter which gets the value of the register read.
  154. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  155. */
  156. int pm8xxx_read_register(u16 addr, u8 *value)
  157. {
  158. struct pm8xxx_misc_chip *chip;
  159. unsigned long flags;
  160. int rc = 0;
  161. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  162. /* Loop over all attached PMICs and call specific functions for them. */
  163. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  164. switch (chip->version) {
  165. case PM8XXX_VERSION_8921:
  166. rc = pm8xxx_readb(chip->dev->parent, addr, value);
  167. if (rc) {
  168. pr_err("pm8xxx_readb(0x%03X) failed, rc=%d\n",
  169. addr, rc);
  170. break;
  171. }
  172. default:
  173. break;
  174. }
  175. }
  176. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  177. return rc;
  178. }
  179. EXPORT_SYMBOL_GPL(pm8xxx_read_register);
  180. /*
  181. * Set an SMPS regulator to be disabled in its CTRL register, but enabled
  182. * in the master enable register. Also set it's pull down enable bit.
  183. * Take care to make sure that the output voltage doesn't change if switching
  184. * from advanced mode to legacy mode.
  185. */
  186. static int
  187. __pm8058_disable_smps_locally_set_pull_down(struct pm8xxx_misc_chip *chip,
  188. u16 ctrl_addr, u16 test2_addr, u16 master_enable_addr,
  189. u8 master_enable_bit)
  190. {
  191. int rc = 0;
  192. u8 vref_sel, vlow_sel, band, vprog, bank, reg;
  193. bank = PM8058_REGULATOR_BANK_SEL(7);
  194. rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank);
  195. if (rc) {
  196. pr_err("%s: pm8xxx_writeb(0x%03X) failed: rc=%d\n", __func__,
  197. test2_addr, rc);
  198. goto done;
  199. }
  200. rc = pm8xxx_readb(chip->dev->parent, test2_addr, &reg);
  201. if (rc) {
  202. pr_err("%s: FAIL pm8xxx_readb(0x%03X): rc=%d\n",
  203. __func__, test2_addr, rc);
  204. goto done;
  205. }
  206. /* Check if in advanced mode. */
  207. if ((reg & PM8058_SMPS_ADVANCED_MODE_MASK) ==
  208. PM8058_SMPS_ADVANCED_MODE) {
  209. /* Determine current output voltage. */
  210. rc = pm8xxx_readb(chip->dev->parent, ctrl_addr, &reg);
  211. if (rc) {
  212. pr_err("%s: FAIL pm8xxx_readb(0x%03X): rc=%d\n",
  213. __func__, ctrl_addr, rc);
  214. goto done;
  215. }
  216. band = (reg & PM8058_SMPS_ADVANCED_BAND_MASK)
  217. >> PM8058_SMPS_ADVANCED_BAND_SHIFT;
  218. switch (band) {
  219. case 3:
  220. vref_sel = 0;
  221. vlow_sel = 0;
  222. break;
  223. case 2:
  224. vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
  225. vlow_sel = 0;
  226. break;
  227. case 1:
  228. vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
  229. vlow_sel = PM8058_SMPS_LEGACY_VLOW_SEL;
  230. break;
  231. default:
  232. pr_err("%s: regulator already disabled\n", __func__);
  233. return -EPERM;
  234. }
  235. vprog = (reg & PM8058_SMPS_ADVANCED_VPROG_MASK);
  236. /* Round up if fine step is in use. */
  237. vprog = (vprog + 1) >> 1;
  238. if (vprog > PM8058_SMPS_LEGACY_VPROG_MASK)
  239. vprog = PM8058_SMPS_LEGACY_VPROG_MASK;
  240. /* Set VLOW_SEL bit. */
  241. bank = PM8058_REGULATOR_BANK_SEL(1);
  242. rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank);
  243. if (rc) {
  244. pr_err("%s: FAIL pm8xxx_writeb(0x%03X): rc=%d\n",
  245. __func__, test2_addr, rc);
  246. goto done;
  247. }
  248. rc = pm8xxx_misc_masked_write(chip, test2_addr,
  249. PM8058_REGULATOR_BANK_WRITE | PM8058_REGULATOR_BANK_MASK
  250. | PM8058_SMPS_LEGACY_VLOW_SEL,
  251. PM8058_REGULATOR_BANK_WRITE |
  252. PM8058_REGULATOR_BANK_SEL(1) | vlow_sel);
  253. if (rc)
  254. goto done;
  255. /* Switch to legacy mode */
  256. bank = PM8058_REGULATOR_BANK_SEL(7);
  257. rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank);
  258. if (rc) {
  259. pr_err("%s: FAIL pm8xxx_writeb(0x%03X): rc=%d\n",
  260. __func__, test2_addr, rc);
  261. goto done;
  262. }
  263. rc = pm8xxx_misc_masked_write(chip, test2_addr,
  264. PM8058_REGULATOR_BANK_WRITE |
  265. PM8058_REGULATOR_BANK_MASK |
  266. PM8058_SMPS_ADVANCED_MODE_MASK,
  267. PM8058_REGULATOR_BANK_WRITE |
  268. PM8058_REGULATOR_BANK_SEL(7) |
  269. PM8058_SMPS_LEGACY_MODE);
  270. if (rc)
  271. goto done;
  272. /* Enable locally, enable pull down, keep voltage the same. */
  273. rc = pm8xxx_misc_masked_write(chip, ctrl_addr,
  274. PM8058_REGULATOR_ENABLE_MASK |
  275. PM8058_REGULATOR_PULL_DOWN_MASK |
  276. PM8058_SMPS_LEGACY_VREF_SEL |
  277. PM8058_SMPS_LEGACY_VPROG_MASK,
  278. PM8058_REGULATOR_ENABLE | PM8058_REGULATOR_PULL_DOWN_EN
  279. | vref_sel | vprog);
  280. if (rc)
  281. goto done;
  282. }
  283. /* Enable in master control register. */
  284. rc = pm8xxx_misc_masked_write(chip, master_enable_addr,
  285. master_enable_bit, master_enable_bit);
  286. if (rc)
  287. goto done;
  288. /* Disable locally and enable pull down. */
  289. rc = pm8xxx_misc_masked_write(chip, ctrl_addr,
  290. PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK,
  291. PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN);
  292. done:
  293. return rc;
  294. }
  295. static int
  296. __pm8058_disable_ldo_locally_set_pull_down(struct pm8xxx_misc_chip *chip,
  297. u16 ctrl_addr, u16 master_enable_addr, u8 master_enable_bit)
  298. {
  299. int rc;
  300. /* Enable LDO in master control register. */
  301. rc = pm8xxx_misc_masked_write(chip, master_enable_addr,
  302. master_enable_bit, master_enable_bit);
  303. if (rc)
  304. goto done;
  305. /* Disable LDO in CTRL register and set pull down */
  306. rc = pm8xxx_misc_masked_write(chip, ctrl_addr,
  307. PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK,
  308. PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN);
  309. done:
  310. return rc;
  311. }
  312. static int __pm8018_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset)
  313. {
  314. int rc;
  315. /* Enable SMPL if resetting is desired. */
  316. rc = pm8xxx_misc_masked_write(chip, REG_PM8018_SLEEP_CTRL,
  317. SLEEP_CTRL_SMPL_EN_MASK,
  318. (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF));
  319. if (rc) {
  320. pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc);
  321. return rc;
  322. }
  323. /*
  324. * Select action to perform (reset or shutdown) when PS_HOLD goes low.
  325. * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that
  326. * USB charging is enabled.
  327. */
  328. rc = pm8xxx_misc_masked_write(chip, REG_PM8XXX_PON_CTRL_1,
  329. PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN
  330. | PON_CTRL_1_WD_EN_MASK,
  331. PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN
  332. | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF));
  333. if (rc)
  334. pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc);
  335. return rc;
  336. }
  337. static int __pm8058_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset)
  338. {
  339. int rc;
  340. /* When shutting down, enable active pulldowns on important rails. */
  341. if (!reset) {
  342. /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */
  343. __pm8058_disable_smps_locally_set_pull_down(chip,
  344. REG_PM8058_S0_CTRL, REG_PM8058_S0_TEST2,
  345. REG_PM8058_VREG_EN_MSM, BIT(7));
  346. __pm8058_disable_smps_locally_set_pull_down(chip,
  347. REG_PM8058_S1_CTRL, REG_PM8058_S1_TEST2,
  348. REG_PM8058_VREG_EN_MSM, BIT(6));
  349. __pm8058_disable_smps_locally_set_pull_down(chip,
  350. REG_PM8058_S3_CTRL, REG_PM8058_S3_TEST2,
  351. REG_PM8058_VREG_EN_GRP_5_4, BIT(7) | BIT(4));
  352. /* Disable LDO 21 locally and set pulldown enable bit. */
  353. __pm8058_disable_ldo_locally_set_pull_down(chip,
  354. REG_PM8058_L21_CTRL, REG_PM8058_VREG_EN_GRP_5_4,
  355. BIT(1));
  356. }
  357. /*
  358. * Fix-up: Set regulator LDO22 to 1.225 V in high power mode. Leave its
  359. * pull-down state intact. This ensures a safe shutdown.
  360. */
  361. rc = pm8xxx_misc_masked_write(chip, REG_PM8058_L22_CTRL, 0xBF, 0x93);
  362. if (rc) {
  363. pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc);
  364. goto read_write_err;
  365. }
  366. /* Enable SMPL if resetting is desired. */
  367. rc = pm8xxx_misc_masked_write(chip, REG_PM8058_SLEEP_CTRL,
  368. SLEEP_CTRL_SMPL_EN_MASK,
  369. (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF));
  370. if (rc) {
  371. pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc);
  372. goto read_write_err;
  373. }
  374. /*
  375. * Select action to perform (reset or shutdown) when PS_HOLD goes low.
  376. * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that
  377. * USB charging is enabled.
  378. */
  379. rc = pm8xxx_misc_masked_write(chip, REG_PM8XXX_PON_CTRL_1,
  380. PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN
  381. | PON_CTRL_1_WD_EN_MASK,
  382. PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN
  383. | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF));
  384. if (rc) {
  385. pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc);
  386. goto read_write_err;
  387. }
  388. read_write_err:
  389. return rc;
  390. }
  391. static int __pm8901_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset)
  392. {
  393. int rc = 0, i;
  394. u8 pmr_addr[4] = {
  395. REG_PM8901_REGULATOR_S2_PMR,
  396. REG_PM8901_REGULATOR_S3_PMR,
  397. REG_PM8901_REGULATOR_S4_PMR,
  398. REG_PM8901_REGULATOR_S1_PMR,
  399. };
  400. /* Fix-up: Turn off regulators S1, S2, S3, S4 when shutting down. */
  401. if (!reset) {
  402. for (i = 0; i < 4; i++) {
  403. rc = pm8xxx_misc_masked_write(chip, pmr_addr[i],
  404. PM8901_REGULATOR_PMR_STATE_MASK,
  405. PM8901_REGULATOR_PMR_STATE_OFF);
  406. if (rc) {
  407. pr_err("pm8xxx_misc_masked_write failed, "
  408. "rc=%d\n", rc);
  409. goto read_write_err;
  410. }
  411. mdelay(PM8901_DELAY_AFTER_REG_DISABLE_MS);
  412. }
  413. }
  414. read_write_err:
  415. mdelay(PM8901_DELAY_BEFORE_SHUTDOWN_MS);
  416. return rc;
  417. }
  418. static int __pm8921_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset)
  419. {
  420. int rc;
  421. /* Enable SMPL if resetting is desired. */
  422. rc = pm8xxx_misc_masked_write(chip, REG_PM8921_SLEEP_CTRL,
  423. SLEEP_CTRL_SMPL_EN_MASK,
  424. (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF));
  425. if (rc) {
  426. pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc);
  427. goto read_write_err;
  428. }
  429. /*
  430. * Select action to perform (reset or shutdown) when PS_HOLD goes low.
  431. * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that
  432. * USB charging is enabled.
  433. */
  434. rc = pm8xxx_misc_masked_write(chip, REG_PM8XXX_PON_CTRL_1,
  435. PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN
  436. | PON_CTRL_1_WD_EN_MASK,
  437. PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN
  438. | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF));
  439. if (rc) {
  440. pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc);
  441. goto read_write_err;
  442. }
  443. read_write_err:
  444. return rc;
  445. }
  446. /**
  447. * pm8xxx_reset_pwr_off - switch all PM8XXX PMIC chips attached to the system to
  448. * either reset or shutdown when they are turned off
  449. * @reset: 0 = shudown the PMICs, 1 = shutdown and then restart the PMICs
  450. *
  451. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  452. */
  453. int pm8xxx_reset_pwr_off(int reset)
  454. {
  455. struct pm8xxx_misc_chip *chip;
  456. unsigned long flags;
  457. int rc = 0;
  458. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  459. /* Loop over all attached PMICs and call specific functions for them. */
  460. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  461. switch (chip->version) {
  462. case PM8XXX_VERSION_8018:
  463. rc = __pm8018_reset_pwr_off(chip, reset);
  464. break;
  465. case PM8XXX_VERSION_8058:
  466. rc = __pm8058_reset_pwr_off(chip, reset);
  467. break;
  468. case PM8XXX_VERSION_8901:
  469. rc = __pm8901_reset_pwr_off(chip, reset);
  470. break;
  471. case PM8XXX_VERSION_8038:
  472. case PM8XXX_VERSION_8917:
  473. case PM8XXX_VERSION_8921:
  474. rc = __pm8921_reset_pwr_off(chip, reset);
  475. break;
  476. default:
  477. /* PMIC doesn't have reset_pwr_off; do nothing. */
  478. break;
  479. }
  480. if (rc) {
  481. pr_err("reset_pwr_off failed, rc=%d\n", rc);
  482. break;
  483. }
  484. }
  485. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  486. return rc;
  487. }
  488. EXPORT_SYMBOL_GPL(pm8xxx_reset_pwr_off);
  489. /**
  490. * pm8xxx_smpl_control - enables/disables SMPL detection
  491. * @enable: 0 = shutdown PMIC on power loss, 1 = reset PMIC on power loss
  492. *
  493. * This function enables or disables the Sudden Momentary Power Loss detection
  494. * module. If SMPL detection is enabled, then when a sufficiently long power
  495. * loss event occurs, the PMIC will automatically reset itself. If SMPL
  496. * detection is disabled, then the PMIC will shutdown when power loss occurs.
  497. *
  498. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  499. */
  500. int pm8xxx_smpl_control(int enable)
  501. {
  502. struct pm8xxx_misc_chip *chip;
  503. unsigned long flags;
  504. int rc = 0;
  505. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  506. /* Loop over all attached PMICs and call specific functions for them. */
  507. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  508. switch (chip->version) {
  509. case PM8XXX_VERSION_8018:
  510. rc = pm8xxx_misc_masked_write(chip,
  511. REG_PM8018_SLEEP_CTRL, SLEEP_CTRL_SMPL_EN_MASK,
  512. (enable ? SLEEP_CTRL_SMPL_EN_RESET
  513. : SLEEP_CTRL_SMPL_EN_PWR_OFF));
  514. break;
  515. case PM8XXX_VERSION_8058:
  516. rc = pm8xxx_misc_masked_write(chip,
  517. REG_PM8058_SLEEP_CTRL, SLEEP_CTRL_SMPL_EN_MASK,
  518. (enable ? SLEEP_CTRL_SMPL_EN_RESET
  519. : SLEEP_CTRL_SMPL_EN_PWR_OFF));
  520. break;
  521. case PM8XXX_VERSION_8921:
  522. case PM8XXX_VERSION_8917:
  523. rc = pm8xxx_misc_masked_write(chip,
  524. REG_PM8921_SLEEP_CTRL, SLEEP_CTRL_SMPL_EN_MASK,
  525. (enable ? SLEEP_CTRL_SMPL_EN_RESET
  526. : SLEEP_CTRL_SMPL_EN_PWR_OFF));
  527. break;
  528. default:
  529. /* PMIC doesn't have reset_pwr_off; do nothing. */
  530. break;
  531. }
  532. if (rc) {
  533. pr_err("setting smpl control failed, rc=%d\n", rc);
  534. break;
  535. }
  536. }
  537. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  538. return rc;
  539. }
  540. EXPORT_SYMBOL(pm8xxx_smpl_control);
  541. /**
  542. * pm8xxx_smpl_set_delay - sets the SMPL detection time delay
  543. * @delay: enum value corresponding to delay time
  544. *
  545. * This function sets the time delay of the SMPL detection module. If power
  546. * is reapplied within this interval, then the PMIC reset automatically. The
  547. * SMPL detection module must be enabled for this delay time to take effect.
  548. *
  549. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  550. */
  551. int pm8xxx_smpl_set_delay(enum pm8xxx_smpl_delay delay)
  552. {
  553. struct pm8xxx_misc_chip *chip;
  554. unsigned long flags;
  555. int rc = 0;
  556. if (delay < SLEEP_CTRL_SMPL_SEL_MIN
  557. || delay > SLEEP_CTRL_SMPL_SEL_MAX) {
  558. pr_err("%s: invalid delay specified: %d\n", __func__, delay);
  559. return -EINVAL;
  560. }
  561. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  562. /* Loop over all attached PMICs and call specific functions for them. */
  563. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  564. switch (chip->version) {
  565. case PM8XXX_VERSION_8018:
  566. rc = pm8xxx_misc_masked_write(chip,
  567. REG_PM8018_SLEEP_CTRL, SLEEP_CTRL_SMPL_SEL_MASK,
  568. delay);
  569. break;
  570. case PM8XXX_VERSION_8058:
  571. rc = pm8xxx_misc_masked_write(chip,
  572. REG_PM8058_SLEEP_CTRL, SLEEP_CTRL_SMPL_SEL_MASK,
  573. delay);
  574. break;
  575. case PM8XXX_VERSION_8921:
  576. case PM8XXX_VERSION_8917:
  577. rc = pm8xxx_misc_masked_write(chip,
  578. REG_PM8921_SLEEP_CTRL, SLEEP_CTRL_SMPL_SEL_MASK,
  579. delay);
  580. break;
  581. default:
  582. /* PMIC doesn't have reset_pwr_off; do nothing. */
  583. break;
  584. }
  585. if (rc) {
  586. pr_err("setting smpl delay failed, rc=%d\n", rc);
  587. break;
  588. }
  589. }
  590. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  591. return rc;
  592. }
  593. EXPORT_SYMBOL(pm8xxx_smpl_set_delay);
  594. /**
  595. * pm8xxx_coincell_chg_config - Disables or enables the coincell charger, and
  596. * configures its voltage and resistor settings.
  597. * @chg_config: Holds both voltage and resistor values, and a
  598. * switch to change the state of charger.
  599. * If state is to disable the charger then
  600. * both voltage and resistor are disregarded.
  601. *
  602. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  603. */
  604. int pm8xxx_coincell_chg_config(struct pm8xxx_coincell_chg *chg_config)
  605. {
  606. struct pm8xxx_misc_chip *chip;
  607. unsigned long flags;
  608. u8 reg = 0, voltage, resistor;
  609. int rc = 0;
  610. if (chg_config == NULL) {
  611. pr_err("chg_config is NULL\n");
  612. return -EINVAL;
  613. }
  614. voltage = chg_config->voltage;
  615. resistor = chg_config->resistor;
  616. if (resistor > PM8XXX_COINCELL_RESISTOR_800_OHMS) {
  617. pr_err("Invalid resistor value provided\n");
  618. return -EINVAL;
  619. }
  620. if (voltage < PM8XXX_COINCELL_VOLTAGE_3p2V ||
  621. (voltage > PM8XXX_COINCELL_VOLTAGE_3p0V &&
  622. voltage != PM8XXX_COINCELL_VOLTAGE_2p5V)) {
  623. pr_err("Invalid voltage value provided\n");
  624. return -EINVAL;
  625. }
  626. if (chg_config->state == PM8XXX_COINCELL_CHG_DISABLE) {
  627. reg = 0;
  628. } else {
  629. reg |= voltage;
  630. reg |= (resistor << COINCELL_RESISTOR_SHIFT);
  631. }
  632. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  633. /* Loop over all attached PMICs and call specific functions for them. */
  634. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  635. switch (chip->version) {
  636. case PM8XXX_VERSION_8018:
  637. rc = pm8xxx_writeb(chip->dev->parent,
  638. REG_PM8018_COIN_CHG, reg);
  639. break;
  640. case PM8XXX_VERSION_8058:
  641. rc = pm8xxx_writeb(chip->dev->parent,
  642. REG_PM8058_COIN_CHG, reg);
  643. break;
  644. case PM8XXX_VERSION_8921:
  645. case PM8XXX_VERSION_8917:
  646. rc = pm8xxx_writeb(chip->dev->parent,
  647. REG_PM8921_COIN_CHG, reg);
  648. break;
  649. default:
  650. /* PMIC doesn't have reset_pwr_off; do nothing. */
  651. break;
  652. }
  653. if (rc) {
  654. pr_err("coincell chg. config failed, rc=%d\n", rc);
  655. break;
  656. }
  657. }
  658. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  659. return rc;
  660. }
  661. EXPORT_SYMBOL(pm8xxx_coincell_chg_config);
  662. /**
  663. * pm8xxx_watchdog_reset_control - enables/disables watchdog reset detection
  664. * @enable: 0 = shutdown when PS_HOLD goes low, 1 = reset when PS_HOLD goes low
  665. *
  666. * This function enables or disables the PMIC watchdog reset detection feature.
  667. * If watchdog reset detection is enabled, then the PMIC will reset itself
  668. * when PS_HOLD goes low. If it is not enabled, then the PMIC will shutdown
  669. * when PS_HOLD goes low.
  670. *
  671. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  672. */
  673. int pm8xxx_watchdog_reset_control(int enable)
  674. {
  675. struct pm8xxx_misc_chip *chip;
  676. unsigned long flags;
  677. int rc = 0;
  678. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  679. /* Loop over all attached PMICs and call specific functions for them. */
  680. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  681. switch (chip->version) {
  682. case PM8XXX_VERSION_8018:
  683. case PM8XXX_VERSION_8058:
  684. case PM8XXX_VERSION_8921:
  685. case PM8XXX_VERSION_8917:
  686. rc = pm8xxx_misc_masked_write(chip,
  687. REG_PM8XXX_PON_CTRL_1, PON_CTRL_1_WD_EN_MASK,
  688. (enable ? PON_CTRL_1_WD_EN_RESET
  689. : PON_CTRL_1_WD_EN_PWR_OFF));
  690. break;
  691. default:
  692. /* WD reset control not supported */
  693. break;
  694. }
  695. if (rc) {
  696. pr_err("setting WD reset control failed, rc=%d\n", rc);
  697. break;
  698. }
  699. }
  700. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  701. return rc;
  702. }
  703. EXPORT_SYMBOL(pm8xxx_watchdog_reset_control);
  704. /**
  705. * pm8xxx_stay_on - enables stay_on feature
  706. *
  707. * PMIC stay-on feature allows PMIC to ignore MSM PS_HOLD=low
  708. * signal so that some special functions like debugging could be
  709. * performed.
  710. *
  711. * This feature should not be used in any product release.
  712. *
  713. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  714. */
  715. int pm8xxx_stay_on(void)
  716. {
  717. struct pm8xxx_misc_chip *chip;
  718. unsigned long flags;
  719. int rc = 0;
  720. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  721. /* Loop over all attached PMICs and call specific functions for them. */
  722. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  723. switch (chip->version) {
  724. case PM8XXX_VERSION_8018:
  725. case PM8XXX_VERSION_8058:
  726. case PM8XXX_VERSION_8921:
  727. case PM8XXX_VERSION_8917:
  728. rc = pm8xxx_writeb(chip->dev->parent,
  729. REG_PM8XXX_GP_TEST_1, PM8XXX_STAY_ON_CFG);
  730. break;
  731. default:
  732. /* stay on not supported */
  733. break;
  734. }
  735. if (rc) {
  736. pr_err("stay_on failed failed, rc=%d\n", rc);
  737. break;
  738. }
  739. }
  740. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  741. return rc;
  742. }
  743. EXPORT_SYMBOL(pm8xxx_stay_on);
  744. static int
  745. __pm8xxx_hard_reset_config(struct pm8xxx_misc_chip *chip,
  746. enum pm8xxx_pon_config config, u16 pon4_addr, u16 pon5_addr)
  747. {
  748. int rc = 0;
  749. switch (config) {
  750. case PM8XXX_DISABLE_HARD_RESET:
  751. rc = pm8xxx_misc_masked_write(chip, pon5_addr,
  752. PON_CTRL_5_HARD_RESET_EN_MASK,
  753. PON_CTRL_5_HARD_RESET_DIS);
  754. break;
  755. case PM8XXX_SHUTDOWN_ON_HARD_RESET:
  756. rc = pm8xxx_misc_masked_write(chip, pon5_addr,
  757. PON_CTRL_5_HARD_RESET_EN_MASK,
  758. PON_CTRL_5_HARD_RESET_EN);
  759. if (!rc) {
  760. rc = pm8xxx_misc_masked_write(chip, pon4_addr,
  761. PON_CTRL_4_RESET_EN_MASK,
  762. PON_CTRL_4_SHUTDOWN_ON_RESET);
  763. }
  764. break;
  765. case PM8XXX_RESTART_ON_HARD_RESET:
  766. rc = pm8xxx_misc_masked_write(chip, pon5_addr,
  767. PON_CTRL_5_HARD_RESET_EN_MASK,
  768. PON_CTRL_5_HARD_RESET_EN);
  769. if (!rc) {
  770. rc = pm8xxx_misc_masked_write(chip, pon4_addr,
  771. PON_CTRL_4_RESET_EN_MASK,
  772. PON_CTRL_4_RESTART_ON_RESET);
  773. }
  774. break;
  775. default:
  776. rc = -EINVAL;
  777. break;
  778. }
  779. return rc;
  780. }
  781. /**
  782. * pm8xxx_hard_reset_config - Allows different reset configurations
  783. *
  784. * config = PM8XXX_DISABLE_HARD_RESET to disable hard reset
  785. * = PM8XXX_SHUTDOWN_ON_HARD_RESET to turn off the system on hard reset
  786. * = PM8XXX_RESTART_ON_HARD_RESET to restart the system on hard reset
  787. *
  788. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  789. */
  790. int pm8xxx_hard_reset_config(enum pm8xxx_pon_config config)
  791. {
  792. struct pm8xxx_misc_chip *chip;
  793. unsigned long flags;
  794. int rc = 0;
  795. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  796. /* Loop over all attached PMICs and call specific functions for them. */
  797. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  798. switch (chip->version) {
  799. case PM8XXX_VERSION_8018:
  800. __pm8xxx_hard_reset_config(chip, config,
  801. REG_PM8018_PON_CNTL_4, REG_PM8018_PON_CNTL_5);
  802. break;
  803. case PM8XXX_VERSION_8058:
  804. __pm8xxx_hard_reset_config(chip, config,
  805. REG_PM8058_PON_CNTL_4, REG_PM8058_PON_CNTL_5);
  806. break;
  807. case PM8XXX_VERSION_8901:
  808. __pm8xxx_hard_reset_config(chip, config,
  809. REG_PM8901_PON_CNTL_4, REG_PM8901_PON_CNTL_5);
  810. break;
  811. case PM8XXX_VERSION_8921:
  812. case PM8XXX_VERSION_8917:
  813. __pm8xxx_hard_reset_config(chip, config,
  814. REG_PM8921_PON_CNTL_4, REG_PM8921_PON_CNTL_5);
  815. break;
  816. default:
  817. /* hard reset config. no supported */
  818. break;
  819. }
  820. if (rc) {
  821. pr_err("hard reset config. failed, rc=%d\n", rc);
  822. break;
  823. }
  824. }
  825. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  826. return rc;
  827. }
  828. EXPORT_SYMBOL(pm8xxx_hard_reset_config);
  829. /* Handle the OSC_HALT interrupt: 32 kHz XTAL oscillator has stopped. */
  830. static irqreturn_t pm8xxx_osc_halt_isr(int irq, void *data)
  831. {
  832. struct pm8xxx_misc_chip *chip = data;
  833. u64 count = 0;
  834. if (chip) {
  835. chip->osc_halt_count++;
  836. count = chip->osc_halt_count;
  837. }
  838. pr_crit("%s: OSC_HALT interrupt has triggered, 32 kHz XTAL oscillator"
  839. " has halted (%llu)!\n", __func__, count);
  840. return IRQ_HANDLED;
  841. }
  842. /**
  843. * pm8xxx_uart_gpio_mux_ctrl - Mux configuration to select the UART
  844. *
  845. * @uart_path_sel: Input argument to select either UART1/2/3
  846. *
  847. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  848. */
  849. int pm8xxx_uart_gpio_mux_ctrl(enum pm8xxx_uart_path_sel uart_path_sel)
  850. {
  851. struct pm8xxx_misc_chip *chip;
  852. unsigned long flags;
  853. int rc = 0;
  854. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  855. /* Loop over all attached PMICs and call specific functions for them. */
  856. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  857. switch (chip->version) {
  858. case PM8XXX_VERSION_8018:
  859. case PM8XXX_VERSION_8058:
  860. case PM8XXX_VERSION_8921:
  861. case PM8XXX_VERSION_8917:
  862. rc = pm8xxx_misc_masked_write(chip,
  863. REG_PM8XXX_GPIO_MUX_CTRL, UART_PATH_SEL_MASK,
  864. uart_path_sel << UART_PATH_SEL_SHIFT);
  865. break;
  866. default:
  867. /* Functionality not supported */
  868. break;
  869. }
  870. if (rc) {
  871. pr_err("uart_gpio_mux_ctrl failed, rc=%d\n", rc);
  872. break;
  873. }
  874. }
  875. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  876. return rc;
  877. }
  878. EXPORT_SYMBOL(pm8xxx_uart_gpio_mux_ctrl);
  879. /**
  880. * pm8xxx_usb_id_pullup - Control a pullup for USB ID
  881. *
  882. * @enable: enable (1) or disable (0) the pullup
  883. *
  884. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  885. */
  886. int pm8xxx_usb_id_pullup(int enable)
  887. {
  888. struct pm8xxx_misc_chip *chip;
  889. unsigned long flags;
  890. int rc = -ENXIO;
  891. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  892. /* Loop over all attached PMICs and call specific functions for them. */
  893. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  894. switch (chip->version) {
  895. case PM8XXX_VERSION_8921:
  896. case PM8XXX_VERSION_8922:
  897. case PM8XXX_VERSION_8917:
  898. case PM8XXX_VERSION_8038:
  899. rc = pm8xxx_misc_masked_write(chip,
  900. REG_PM8XXX_GPIO_MUX_CTRL, USB_ID_PU_EN_MASK,
  901. enable << USB_ID_PU_EN_SHIFT);
  902. if (rc)
  903. pr_err("Fail: reg=%x, rc=%d\n",
  904. REG_PM8XXX_GPIO_MUX_CTRL, rc);
  905. break;
  906. default:
  907. /* Functionality not supported */
  908. break;
  909. }
  910. }
  911. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  912. return rc;
  913. }
  914. EXPORT_SYMBOL(pm8xxx_usb_id_pullup);
  915. static int __pm8901_preload_dVdd(struct pm8xxx_misc_chip *chip)
  916. {
  917. int rc;
  918. /* dVdd preloading is not needed for PMIC PM8901 rev 2.3 and beyond. */
  919. if (pm8xxx_get_revision(chip->dev->parent) >= PM8XXX_REVISION_8901_2p3)
  920. return 0;
  921. rc = pm8xxx_writeb(chip->dev->parent, 0x0BD, 0x0F);
  922. if (rc)
  923. pr_err("pm8xxx_writeb failed for 0x0BD, rc=%d\n", rc);
  924. rc = pm8xxx_writeb(chip->dev->parent, 0x001, 0xB4);
  925. if (rc)
  926. pr_err("pm8xxx_writeb failed for 0x001, rc=%d\n", rc);
  927. pr_info("dVdd preloaded\n");
  928. return rc;
  929. }
  930. /**
  931. * pm8xxx_preload_dVdd - preload the dVdd regulator during off state.
  932. *
  933. * This can help to reduce fluctuations in the dVdd voltage during startup
  934. * at the cost of additional off state current draw.
  935. *
  936. * This API should only be called if dVdd startup issues are suspected.
  937. *
  938. * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
  939. */
  940. int pm8xxx_preload_dVdd(void)
  941. {
  942. struct pm8xxx_misc_chip *chip;
  943. unsigned long flags;
  944. int rc = 0;
  945. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  946. /* Loop over all attached PMICs and call specific functions for them. */
  947. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  948. switch (chip->version) {
  949. case PM8XXX_VERSION_8901:
  950. rc = __pm8901_preload_dVdd(chip);
  951. break;
  952. default:
  953. /* PMIC doesn't have preload_dVdd; do nothing. */
  954. break;
  955. }
  956. if (rc) {
  957. pr_err("preload_dVdd failed, rc=%d\n", rc);
  958. break;
  959. }
  960. }
  961. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  962. return rc;
  963. }
  964. EXPORT_SYMBOL_GPL(pm8xxx_preload_dVdd);
  965. int pm8xxx_aux_clk_control(enum pm8xxx_aux_clk_id clk_id,
  966. enum pm8xxx_aux_clk_div divider, bool enable)
  967. {
  968. struct pm8xxx_misc_chip *chip;
  969. unsigned long flags;
  970. u8 clk_mask = 0, value = 0;
  971. if (clk_id == CLK_MP3_1) {
  972. clk_mask = MP3_1_MASK;
  973. value = divider << MP3_1_SHIFT;
  974. } else if (clk_id == CLK_MP3_2) {
  975. clk_mask = MP3_2_MASK;
  976. value = divider << MP3_2_SHIFT;
  977. } else {
  978. pr_err("Invalid clock id of %d\n", clk_id);
  979. return -EINVAL;
  980. }
  981. if (!enable)
  982. value = 0;
  983. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  984. /* Loop over all attached PMICs and call specific functions for them. */
  985. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  986. switch (chip->version) {
  987. case PM8XXX_VERSION_8038:
  988. case PM8XXX_VERSION_8921:
  989. case PM8XXX_VERSION_8917:
  990. pm8xxx_misc_masked_write(chip,
  991. REG_PM8XXX_XO_CNTRL_2, clk_mask, value);
  992. break;
  993. default:
  994. /* Functionality not supported */
  995. break;
  996. }
  997. }
  998. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  999. return 0;
  1000. }
  1001. EXPORT_SYMBOL_GPL(pm8xxx_aux_clk_control);
  1002. int pm8xxx_hsed_bias_control(enum pm8xxx_hsed_bias bias, bool enable)
  1003. {
  1004. struct pm8xxx_misc_chip *chip;
  1005. unsigned long flags;
  1006. int rc = 0;
  1007. u16 addr;
  1008. switch (bias) {
  1009. case PM8XXX_HSED_BIAS0:
  1010. addr = REG_HSED_BIAS0_CNTL2;
  1011. break;
  1012. case PM8XXX_HSED_BIAS1:
  1013. addr = REG_HSED_BIAS1_CNTL2;
  1014. break;
  1015. case PM8XXX_HSED_BIAS2:
  1016. addr = REG_HSED_BIAS2_CNTL2;
  1017. break;
  1018. default:
  1019. pr_err("Invalid BIAS line\n");
  1020. return -EINVAL;
  1021. }
  1022. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  1023. /* Loop over all attached PMICs and call specific functions for them. */
  1024. list_for_each_entry(chip, &pm8xxx_misc_chips, link) {
  1025. switch (chip->version) {
  1026. case PM8XXX_VERSION_8058:
  1027. case PM8XXX_VERSION_8921:
  1028. rc = pm8xxx_misc_masked_write(chip, addr,
  1029. HSED_EN_MASK, enable ? HSED_EN_MASK : 0);
  1030. if (rc < 0)
  1031. pr_err("Enable HSED BIAS failed rc=%d\n", rc);
  1032. break;
  1033. default:
  1034. /* Functionality not supported */
  1035. break;
  1036. }
  1037. }
  1038. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  1039. return rc;
  1040. }
  1041. EXPORT_SYMBOL(pm8xxx_hsed_bias_control);
  1042. static int __devinit pm8xxx_misc_probe(struct platform_device *pdev)
  1043. {
  1044. const struct pm8xxx_misc_platform_data *pdata = pdev->dev.platform_data;
  1045. struct pm8xxx_misc_chip *chip;
  1046. struct pm8xxx_misc_chip *sibling;
  1047. struct list_head *prev;
  1048. unsigned long flags;
  1049. int rc = 0, irq;
  1050. if (!pdata) {
  1051. pr_err("missing platform data\n");
  1052. return -EINVAL;
  1053. }
  1054. chip = kzalloc(sizeof(struct pm8xxx_misc_chip), GFP_KERNEL);
  1055. if (!chip) {
  1056. pr_err("Cannot allocate %d bytes\n",
  1057. sizeof(struct pm8xxx_misc_chip));
  1058. return -ENOMEM;
  1059. }
  1060. chip->dev = &pdev->dev;
  1061. chip->version = pm8xxx_get_version(chip->dev->parent);
  1062. memcpy(&(chip->pdata), pdata, sizeof(struct pm8xxx_misc_platform_data));
  1063. irq = platform_get_irq_byname(pdev, "pm8xxx_osc_halt_irq");
  1064. if (irq > 0) {
  1065. rc = request_any_context_irq(irq, pm8xxx_osc_halt_isr,
  1066. IRQF_TRIGGER_RISING | IRQF_DISABLED,
  1067. "pm8xxx_osc_halt_irq", chip);
  1068. if (rc < 0) {
  1069. pr_err("%s: request_any_context_irq(%d) FAIL: %d\n",
  1070. __func__, irq, rc);
  1071. goto fail_irq;
  1072. }
  1073. }
  1074. /* Insert PMICs in priority order (lowest value first). */
  1075. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  1076. prev = &pm8xxx_misc_chips;
  1077. list_for_each_entry(sibling, &pm8xxx_misc_chips, link) {
  1078. if (chip->pdata.priority < sibling->pdata.priority)
  1079. break;
  1080. else
  1081. prev = &sibling->link;
  1082. }
  1083. list_add(&chip->link, prev);
  1084. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  1085. platform_set_drvdata(pdev, chip);
  1086. return rc;
  1087. fail_irq:
  1088. platform_set_drvdata(pdev, NULL);
  1089. kfree(chip);
  1090. return rc;
  1091. }
  1092. static int __devexit pm8xxx_misc_remove(struct platform_device *pdev)
  1093. {
  1094. struct pm8xxx_misc_chip *chip = platform_get_drvdata(pdev);
  1095. unsigned long flags;
  1096. int irq = platform_get_irq_byname(pdev, "pm8xxx_osc_halt_irq");
  1097. if (irq > 0)
  1098. free_irq(irq, chip);
  1099. spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags);
  1100. list_del(&chip->link);
  1101. spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags);
  1102. platform_set_drvdata(pdev, NULL);
  1103. kfree(chip);
  1104. return 0;
  1105. }
  1106. static struct platform_driver pm8xxx_misc_driver = {
  1107. .probe = pm8xxx_misc_probe,
  1108. .remove = __devexit_p(pm8xxx_misc_remove),
  1109. .driver = {
  1110. .name = PM8XXX_MISC_DEV_NAME,
  1111. .owner = THIS_MODULE,
  1112. },
  1113. };
  1114. static int __init pm8xxx_misc_init(void)
  1115. {
  1116. return platform_driver_register(&pm8xxx_misc_driver);
  1117. }
  1118. postcore_initcall(pm8xxx_misc_init);
  1119. static void __exit pm8xxx_misc_exit(void)
  1120. {
  1121. platform_driver_unregister(&pm8xxx_misc_driver);
  1122. }
  1123. module_exit(pm8xxx_misc_exit);
  1124. MODULE_LICENSE("GPL v2");
  1125. MODULE_DESCRIPTION("PMIC 8XXX misc driver");
  1126. MODULE_VERSION("1.0");
  1127. MODULE_ALIAS("platform:" PM8XXX_MISC_DEV_NAME);