pmic8xxx-pwrkey.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/input.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/log2.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #define PON_CNTL_1 0x1C
  24. #define PON_CNTL_PULL_UP BIT(7)
  25. #define PON_CNTL_TRIG_DELAY_MASK (0x7)
  26. #define PON_CNTL_1_PULL_UP_EN 0xe0
  27. #define PON_CNTL_1_USB_PWR_EN 0x10
  28. #define PON_CNTL_1_WD_EN_RESET 0x08
  29. #define PM8058_SLEEP_CTRL 0x02b
  30. #define PM8921_SLEEP_CTRL 0x10a
  31. #define SLEEP_CTRL_SMPL_EN_RESET 0x04
  32. /* Regulator master enable addresses */
  33. #define REG_PM8058_VREG_EN_MSM 0x018
  34. #define REG_PM8058_VREG_EN_GRP_5_4 0x1c8
  35. /* Regulator control registers for shutdown/reset */
  36. #define PM8058_S0_CTRL 0x004
  37. #define PM8058_S1_CTRL 0x005
  38. #define PM8058_S3_CTRL 0x111
  39. #define PM8058_L21_CTRL 0x120
  40. #define PM8058_L22_CTRL 0x121
  41. #define PM8058_REGULATOR_ENABLE_MASK 0x80
  42. #define PM8058_REGULATOR_ENABLE 0x80
  43. #define PM8058_REGULATOR_DISABLE 0x00
  44. #define PM8058_REGULATOR_PULL_DOWN_MASK 0x40
  45. #define PM8058_REGULATOR_PULL_DOWN_EN 0x40
  46. /* Buck CTRL register */
  47. #define PM8058_SMPS_LEGACY_VREF_SEL 0x20
  48. #define PM8058_SMPS_LEGACY_VPROG_MASK 0x1f
  49. #define PM8058_SMPS_ADVANCED_BAND_MASK 0xC0
  50. #define PM8058_SMPS_ADVANCED_BAND_SHIFT 6
  51. #define PM8058_SMPS_ADVANCED_VPROG_MASK 0x3f
  52. /* Buck TEST2 registers for shutdown/reset */
  53. #define PM8058_S0_TEST2 0x084
  54. #define PM8058_S1_TEST2 0x085
  55. #define PM8058_S3_TEST2 0x11a
  56. #define PM8058_REGULATOR_BANK_WRITE 0x80
  57. #define PM8058_REGULATOR_BANK_MASK 0x70
  58. #define PM8058_REGULATOR_BANK_SHIFT 4
  59. #define PM8058_REGULATOR_BANK_SEL(n) ((n) << PM8058_REGULATOR_BANK_SHIFT)
  60. /* Buck TEST2 register bank 1 */
  61. #define PM8058_SMPS_LEGACY_VLOW_SEL 0x01
  62. /* Buck TEST2 register bank 7 */
  63. #define PM8058_SMPS_ADVANCED_MODE_MASK 0x02
  64. #define PM8058_SMPS_ADVANCED_MODE 0x02
  65. #define PM8058_SMPS_LEGACY_MODE 0x00
  66. /**
  67. * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information
  68. * @key_press_irq: key press irq number
  69. * @regmap: device regmap
  70. * @shutdown_fn: shutdown configuration function
  71. */
  72. struct pmic8xxx_pwrkey {
  73. int key_press_irq;
  74. struct regmap *regmap;
  75. int (*shutdown_fn)(struct pmic8xxx_pwrkey *, bool);
  76. };
  77. static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
  78. {
  79. struct input_dev *pwr = _pwr;
  80. input_report_key(pwr, KEY_POWER, 1);
  81. input_sync(pwr);
  82. return IRQ_HANDLED;
  83. }
  84. static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
  85. {
  86. struct input_dev *pwr = _pwr;
  87. input_report_key(pwr, KEY_POWER, 0);
  88. input_sync(pwr);
  89. return IRQ_HANDLED;
  90. }
  91. static int __maybe_unused pmic8xxx_pwrkey_suspend(struct device *dev)
  92. {
  93. struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
  94. if (device_may_wakeup(dev))
  95. enable_irq_wake(pwrkey->key_press_irq);
  96. return 0;
  97. }
  98. static int __maybe_unused pmic8xxx_pwrkey_resume(struct device *dev)
  99. {
  100. struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
  101. if (device_may_wakeup(dev))
  102. disable_irq_wake(pwrkey->key_press_irq);
  103. return 0;
  104. }
  105. static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
  106. pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
  107. static void pmic8xxx_pwrkey_shutdown(struct platform_device *pdev)
  108. {
  109. struct pmic8xxx_pwrkey *pwrkey = platform_get_drvdata(pdev);
  110. int error;
  111. u8 mask, val;
  112. bool reset = system_state == SYSTEM_RESTART;
  113. if (pwrkey->shutdown_fn) {
  114. error = pwrkey->shutdown_fn(pwrkey, reset);
  115. if (error)
  116. return;
  117. }
  118. /*
  119. * Select action to perform (reset or shutdown) when PS_HOLD goes low.
  120. * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that
  121. * USB charging is enabled.
  122. */
  123. mask = PON_CNTL_1_PULL_UP_EN | PON_CNTL_1_USB_PWR_EN;
  124. mask |= PON_CNTL_1_WD_EN_RESET;
  125. val = mask;
  126. if (!reset)
  127. val &= ~PON_CNTL_1_WD_EN_RESET;
  128. regmap_update_bits(pwrkey->regmap, PON_CNTL_1, mask, val);
  129. }
  130. /*
  131. * Set an SMPS regulator to be disabled in its CTRL register, but enabled
  132. * in the master enable register. Also set it's pull down enable bit.
  133. * Take care to make sure that the output voltage doesn't change if switching
  134. * from advanced mode to legacy mode.
  135. */
  136. static int pm8058_disable_smps_locally_set_pull_down(struct regmap *regmap,
  137. u16 ctrl_addr, u16 test2_addr, u16 master_enable_addr,
  138. u8 master_enable_bit)
  139. {
  140. int error;
  141. u8 vref_sel, vlow_sel, band, vprog, bank;
  142. unsigned int reg;
  143. bank = PM8058_REGULATOR_BANK_SEL(7);
  144. error = regmap_write(regmap, test2_addr, bank);
  145. if (error)
  146. return error;
  147. error = regmap_read(regmap, test2_addr, &reg);
  148. if (error)
  149. return error;
  150. reg &= PM8058_SMPS_ADVANCED_MODE_MASK;
  151. /* Check if in advanced mode. */
  152. if (reg == PM8058_SMPS_ADVANCED_MODE) {
  153. /* Determine current output voltage. */
  154. error = regmap_read(regmap, ctrl_addr, &reg);
  155. if (error)
  156. return error;
  157. band = reg & PM8058_SMPS_ADVANCED_BAND_MASK;
  158. band >>= PM8058_SMPS_ADVANCED_BAND_SHIFT;
  159. switch (band) {
  160. case 3:
  161. vref_sel = 0;
  162. vlow_sel = 0;
  163. break;
  164. case 2:
  165. vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
  166. vlow_sel = 0;
  167. break;
  168. case 1:
  169. vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
  170. vlow_sel = PM8058_SMPS_LEGACY_VLOW_SEL;
  171. break;
  172. default:
  173. pr_err("%s: regulator already disabled\n", __func__);
  174. return -EPERM;
  175. }
  176. vprog = reg & PM8058_SMPS_ADVANCED_VPROG_MASK;
  177. /* Round up if fine step is in use. */
  178. vprog = (vprog + 1) >> 1;
  179. if (vprog > PM8058_SMPS_LEGACY_VPROG_MASK)
  180. vprog = PM8058_SMPS_LEGACY_VPROG_MASK;
  181. /* Set VLOW_SEL bit. */
  182. bank = PM8058_REGULATOR_BANK_SEL(1);
  183. error = regmap_write(regmap, test2_addr, bank);
  184. if (error)
  185. return error;
  186. error = regmap_update_bits(regmap, test2_addr,
  187. PM8058_REGULATOR_BANK_WRITE | PM8058_REGULATOR_BANK_MASK
  188. | PM8058_SMPS_LEGACY_VLOW_SEL,
  189. PM8058_REGULATOR_BANK_WRITE |
  190. PM8058_REGULATOR_BANK_SEL(1) | vlow_sel);
  191. if (error)
  192. return error;
  193. /* Switch to legacy mode */
  194. bank = PM8058_REGULATOR_BANK_SEL(7);
  195. error = regmap_write(regmap, test2_addr, bank);
  196. if (error)
  197. return error;
  198. error = regmap_update_bits(regmap, test2_addr,
  199. PM8058_REGULATOR_BANK_WRITE |
  200. PM8058_REGULATOR_BANK_MASK |
  201. PM8058_SMPS_ADVANCED_MODE_MASK,
  202. PM8058_REGULATOR_BANK_WRITE |
  203. PM8058_REGULATOR_BANK_SEL(7) |
  204. PM8058_SMPS_LEGACY_MODE);
  205. if (error)
  206. return error;
  207. /* Enable locally, enable pull down, keep voltage the same. */
  208. error = regmap_update_bits(regmap, ctrl_addr,
  209. PM8058_REGULATOR_ENABLE_MASK |
  210. PM8058_REGULATOR_PULL_DOWN_MASK |
  211. PM8058_SMPS_LEGACY_VREF_SEL |
  212. PM8058_SMPS_LEGACY_VPROG_MASK,
  213. PM8058_REGULATOR_ENABLE | PM8058_REGULATOR_PULL_DOWN_EN
  214. | vref_sel | vprog);
  215. if (error)
  216. return error;
  217. }
  218. /* Enable in master control register. */
  219. error = regmap_update_bits(regmap, master_enable_addr,
  220. master_enable_bit, master_enable_bit);
  221. if (error)
  222. return error;
  223. /* Disable locally and enable pull down. */
  224. return regmap_update_bits(regmap, ctrl_addr,
  225. PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK,
  226. PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN);
  227. }
  228. static int pm8058_disable_ldo_locally_set_pull_down(struct regmap *regmap,
  229. u16 ctrl_addr, u16 master_enable_addr, u8 master_enable_bit)
  230. {
  231. int error;
  232. /* Enable LDO in master control register. */
  233. error = regmap_update_bits(regmap, master_enable_addr,
  234. master_enable_bit, master_enable_bit);
  235. if (error)
  236. return error;
  237. /* Disable LDO in CTRL register and set pull down */
  238. return regmap_update_bits(regmap, ctrl_addr,
  239. PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK,
  240. PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN);
  241. }
  242. static int pm8058_pwrkey_shutdown(struct pmic8xxx_pwrkey *pwrkey, bool reset)
  243. {
  244. int error;
  245. struct regmap *regmap = pwrkey->regmap;
  246. u8 mask, val;
  247. /* When shutting down, enable active pulldowns on important rails. */
  248. if (!reset) {
  249. /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */
  250. pm8058_disable_smps_locally_set_pull_down(regmap,
  251. PM8058_S0_CTRL, PM8058_S0_TEST2,
  252. REG_PM8058_VREG_EN_MSM, BIT(7));
  253. pm8058_disable_smps_locally_set_pull_down(regmap,
  254. PM8058_S1_CTRL, PM8058_S1_TEST2,
  255. REG_PM8058_VREG_EN_MSM, BIT(6));
  256. pm8058_disable_smps_locally_set_pull_down(regmap,
  257. PM8058_S3_CTRL, PM8058_S3_TEST2,
  258. REG_PM8058_VREG_EN_GRP_5_4, BIT(7) | BIT(4));
  259. /* Disable LDO 21 locally and set pulldown enable bit. */
  260. pm8058_disable_ldo_locally_set_pull_down(regmap,
  261. PM8058_L21_CTRL, REG_PM8058_VREG_EN_GRP_5_4,
  262. BIT(1));
  263. }
  264. /*
  265. * Fix-up: Set regulator LDO22 to 1.225 V in high power mode. Leave its
  266. * pull-down state intact. This ensures a safe shutdown.
  267. */
  268. error = regmap_update_bits(regmap, PM8058_L22_CTRL, 0xbf, 0x93);
  269. if (error)
  270. return error;
  271. /* Enable SMPL if resetting is desired */
  272. mask = SLEEP_CTRL_SMPL_EN_RESET;
  273. val = 0;
  274. if (reset)
  275. val = mask;
  276. return regmap_update_bits(regmap, PM8058_SLEEP_CTRL, mask, val);
  277. }
  278. static int pm8921_pwrkey_shutdown(struct pmic8xxx_pwrkey *pwrkey, bool reset)
  279. {
  280. struct regmap *regmap = pwrkey->regmap;
  281. u8 mask = SLEEP_CTRL_SMPL_EN_RESET;
  282. u8 val = 0;
  283. /* Enable SMPL if resetting is desired */
  284. if (reset)
  285. val = mask;
  286. return regmap_update_bits(regmap, PM8921_SLEEP_CTRL, mask, val);
  287. }
  288. static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
  289. {
  290. struct input_dev *pwr;
  291. int key_release_irq = platform_get_irq(pdev, 0);
  292. int key_press_irq = platform_get_irq(pdev, 1);
  293. int err;
  294. unsigned int delay;
  295. unsigned int pon_cntl;
  296. struct regmap *regmap;
  297. struct pmic8xxx_pwrkey *pwrkey;
  298. u32 kpd_delay;
  299. bool pull_up;
  300. if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay))
  301. kpd_delay = 15625;
  302. /* Valid range of pwr key trigger delay is 1/64 sec to 2 seconds. */
  303. if (kpd_delay > USEC_PER_SEC * 2 || kpd_delay < USEC_PER_SEC / 64) {
  304. dev_err(&pdev->dev, "invalid power key trigger delay\n");
  305. return -EINVAL;
  306. }
  307. pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up");
  308. regmap = dev_get_regmap(pdev->dev.parent, NULL);
  309. if (!regmap) {
  310. dev_err(&pdev->dev, "failed to locate regmap for the device\n");
  311. return -ENODEV;
  312. }
  313. pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
  314. if (!pwrkey)
  315. return -ENOMEM;
  316. pwrkey->shutdown_fn = of_device_get_match_data(&pdev->dev);
  317. pwrkey->regmap = regmap;
  318. pwrkey->key_press_irq = key_press_irq;
  319. pwr = devm_input_allocate_device(&pdev->dev);
  320. if (!pwr) {
  321. dev_dbg(&pdev->dev, "Can't allocate power button\n");
  322. return -ENOMEM;
  323. }
  324. input_set_capability(pwr, EV_KEY, KEY_POWER);
  325. pwr->name = "pmic8xxx_pwrkey";
  326. pwr->phys = "pmic8xxx_pwrkey/input0";
  327. delay = (kpd_delay << 6) / USEC_PER_SEC;
  328. delay = ilog2(delay);
  329. err = regmap_read(regmap, PON_CNTL_1, &pon_cntl);
  330. if (err < 0) {
  331. dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err);
  332. return err;
  333. }
  334. pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
  335. pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
  336. if (pull_up)
  337. pon_cntl |= PON_CNTL_PULL_UP;
  338. else
  339. pon_cntl &= ~PON_CNTL_PULL_UP;
  340. err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
  341. if (err < 0) {
  342. dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err);
  343. return err;
  344. }
  345. err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq,
  346. IRQF_TRIGGER_RISING,
  347. "pmic8xxx_pwrkey_press", pwr);
  348. if (err) {
  349. dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
  350. key_press_irq, err);
  351. return err;
  352. }
  353. err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq,
  354. IRQF_TRIGGER_RISING,
  355. "pmic8xxx_pwrkey_release", pwr);
  356. if (err) {
  357. dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
  358. key_release_irq, err);
  359. return err;
  360. }
  361. err = input_register_device(pwr);
  362. if (err) {
  363. dev_err(&pdev->dev, "Can't register power key: %d\n", err);
  364. return err;
  365. }
  366. platform_set_drvdata(pdev, pwrkey);
  367. device_init_wakeup(&pdev->dev, 1);
  368. return 0;
  369. }
  370. static int pmic8xxx_pwrkey_remove(struct platform_device *pdev)
  371. {
  372. device_init_wakeup(&pdev->dev, 0);
  373. return 0;
  374. }
  375. static const struct of_device_id pm8xxx_pwr_key_id_table[] = {
  376. { .compatible = "qcom,pm8058-pwrkey", .data = &pm8058_pwrkey_shutdown },
  377. { .compatible = "qcom,pm8921-pwrkey", .data = &pm8921_pwrkey_shutdown },
  378. { }
  379. };
  380. MODULE_DEVICE_TABLE(of, pm8xxx_pwr_key_id_table);
  381. static struct platform_driver pmic8xxx_pwrkey_driver = {
  382. .probe = pmic8xxx_pwrkey_probe,
  383. .remove = pmic8xxx_pwrkey_remove,
  384. .shutdown = pmic8xxx_pwrkey_shutdown,
  385. .driver = {
  386. .name = "pm8xxx-pwrkey",
  387. .pm = &pm8xxx_pwr_key_pm_ops,
  388. .of_match_table = pm8xxx_pwr_key_id_table,
  389. },
  390. };
  391. module_platform_driver(pmic8xxx_pwrkey_driver);
  392. MODULE_ALIAS("platform:pmic8xxx_pwrkey");
  393. MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
  394. MODULE_LICENSE("GPL v2");
  395. MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");