intel_soc_pmic_bxtwc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * MFD core driver for Intel Broxton Whiskey Cove PMIC
  3. *
  4. * Copyright (C) 2015 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/acpi.h>
  17. #include <linux/err.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/mfd/intel_bxtwc.h>
  23. #include <asm/intel_pmc_ipc.h>
  24. /* PMIC device registers */
  25. #define REG_ADDR_MASK 0xFF00
  26. #define REG_ADDR_SHIFT 8
  27. #define REG_OFFSET_MASK 0xFF
  28. /* Interrupt Status Registers */
  29. #define BXTWC_IRQLVL1 0x4E02
  30. #define BXTWC_PWRBTNIRQ 0x4E03
  31. #define BXTWC_THRM0IRQ 0x4E04
  32. #define BXTWC_THRM1IRQ 0x4E05
  33. #define BXTWC_THRM2IRQ 0x4E06
  34. #define BXTWC_BCUIRQ 0x4E07
  35. #define BXTWC_ADCIRQ 0x4E08
  36. #define BXTWC_CHGR0IRQ 0x4E09
  37. #define BXTWC_CHGR1IRQ 0x4E0A
  38. #define BXTWC_GPIOIRQ0 0x4E0B
  39. #define BXTWC_GPIOIRQ1 0x4E0C
  40. #define BXTWC_CRITIRQ 0x4E0D
  41. /* Interrupt MASK Registers */
  42. #define BXTWC_MIRQLVL1 0x4E0E
  43. #define BXTWC_MPWRTNIRQ 0x4E0F
  44. #define BXTWC_MIRQLVL1_MCHGR BIT(5)
  45. #define BXTWC_MTHRM0IRQ 0x4E12
  46. #define BXTWC_MTHRM1IRQ 0x4E13
  47. #define BXTWC_MTHRM2IRQ 0x4E14
  48. #define BXTWC_MBCUIRQ 0x4E15
  49. #define BXTWC_MADCIRQ 0x4E16
  50. #define BXTWC_MCHGR0IRQ 0x4E17
  51. #define BXTWC_MCHGR1IRQ 0x4E18
  52. #define BXTWC_MGPIO0IRQ 0x4E19
  53. #define BXTWC_MGPIO1IRQ 0x4E1A
  54. #define BXTWC_MCRITIRQ 0x4E1B
  55. /* Whiskey Cove PMIC share same ACPI ID between different platforms */
  56. #define BROXTON_PMIC_WC_HRV 4
  57. /* Manage in two IRQ chips since mask registers are not consecutive */
  58. enum bxtwc_irqs {
  59. /* Level 1 */
  60. BXTWC_PWRBTN_LVL1_IRQ = 0,
  61. BXTWC_TMU_LVL1_IRQ,
  62. BXTWC_THRM_LVL1_IRQ,
  63. BXTWC_BCU_LVL1_IRQ,
  64. BXTWC_ADC_LVL1_IRQ,
  65. BXTWC_CHGR_LVL1_IRQ,
  66. BXTWC_GPIO_LVL1_IRQ,
  67. BXTWC_CRIT_LVL1_IRQ,
  68. /* Level 2 */
  69. BXTWC_PWRBTN_IRQ,
  70. };
  71. enum bxtwc_irqs_level2 {
  72. /* Level 2 */
  73. BXTWC_THRM0_IRQ = 0,
  74. BXTWC_THRM1_IRQ,
  75. BXTWC_THRM2_IRQ,
  76. BXTWC_BCU_IRQ,
  77. BXTWC_ADC_IRQ,
  78. BXTWC_USBC_IRQ,
  79. BXTWC_CHGR0_IRQ,
  80. BXTWC_CHGR1_IRQ,
  81. BXTWC_GPIO0_IRQ,
  82. BXTWC_GPIO1_IRQ,
  83. BXTWC_CRIT_IRQ,
  84. };
  85. static const struct regmap_irq bxtwc_regmap_irqs[] = {
  86. REGMAP_IRQ_REG(BXTWC_PWRBTN_LVL1_IRQ, 0, BIT(0)),
  87. REGMAP_IRQ_REG(BXTWC_TMU_LVL1_IRQ, 0, BIT(1)),
  88. REGMAP_IRQ_REG(BXTWC_THRM_LVL1_IRQ, 0, BIT(2)),
  89. REGMAP_IRQ_REG(BXTWC_BCU_LVL1_IRQ, 0, BIT(3)),
  90. REGMAP_IRQ_REG(BXTWC_ADC_LVL1_IRQ, 0, BIT(4)),
  91. REGMAP_IRQ_REG(BXTWC_CHGR_LVL1_IRQ, 0, BIT(5)),
  92. REGMAP_IRQ_REG(BXTWC_GPIO_LVL1_IRQ, 0, BIT(6)),
  93. REGMAP_IRQ_REG(BXTWC_CRIT_LVL1_IRQ, 0, BIT(7)),
  94. REGMAP_IRQ_REG(BXTWC_PWRBTN_IRQ, 1, 0x03),
  95. };
  96. static const struct regmap_irq bxtwc_regmap_irqs_level2[] = {
  97. REGMAP_IRQ_REG(BXTWC_THRM0_IRQ, 0, 0xff),
  98. REGMAP_IRQ_REG(BXTWC_THRM1_IRQ, 1, 0xbf),
  99. REGMAP_IRQ_REG(BXTWC_THRM2_IRQ, 2, 0xff),
  100. REGMAP_IRQ_REG(BXTWC_BCU_IRQ, 3, 0x1f),
  101. REGMAP_IRQ_REG(BXTWC_ADC_IRQ, 4, 0xff),
  102. REGMAP_IRQ_REG(BXTWC_USBC_IRQ, 5, BIT(5)),
  103. REGMAP_IRQ_REG(BXTWC_CHGR0_IRQ, 5, 0x1f),
  104. REGMAP_IRQ_REG(BXTWC_CHGR1_IRQ, 6, 0x1f),
  105. REGMAP_IRQ_REG(BXTWC_GPIO0_IRQ, 7, 0xff),
  106. REGMAP_IRQ_REG(BXTWC_GPIO1_IRQ, 8, 0x3f),
  107. REGMAP_IRQ_REG(BXTWC_CRIT_IRQ, 9, 0x03),
  108. };
  109. static struct regmap_irq_chip bxtwc_regmap_irq_chip = {
  110. .name = "bxtwc_irq_chip",
  111. .status_base = BXTWC_IRQLVL1,
  112. .mask_base = BXTWC_MIRQLVL1,
  113. .irqs = bxtwc_regmap_irqs,
  114. .num_irqs = ARRAY_SIZE(bxtwc_regmap_irqs),
  115. .num_regs = 2,
  116. };
  117. static struct regmap_irq_chip bxtwc_regmap_irq_chip_level2 = {
  118. .name = "bxtwc_irq_chip_level2",
  119. .status_base = BXTWC_THRM0IRQ,
  120. .mask_base = BXTWC_MTHRM0IRQ,
  121. .irqs = bxtwc_regmap_irqs_level2,
  122. .num_irqs = ARRAY_SIZE(bxtwc_regmap_irqs_level2),
  123. .num_regs = 10,
  124. };
  125. static struct resource gpio_resources[] = {
  126. DEFINE_RES_IRQ_NAMED(BXTWC_GPIO0_IRQ, "GPIO0"),
  127. DEFINE_RES_IRQ_NAMED(BXTWC_GPIO1_IRQ, "GPIO1"),
  128. };
  129. static struct resource adc_resources[] = {
  130. DEFINE_RES_IRQ_NAMED(BXTWC_ADC_IRQ, "ADC"),
  131. };
  132. static struct resource usbc_resources[] = {
  133. DEFINE_RES_IRQ(BXTWC_USBC_IRQ),
  134. };
  135. static struct resource charger_resources[] = {
  136. DEFINE_RES_IRQ_NAMED(BXTWC_CHGR0_IRQ, "CHARGER"),
  137. DEFINE_RES_IRQ_NAMED(BXTWC_CHGR1_IRQ, "CHARGER1"),
  138. };
  139. static struct resource thermal_resources[] = {
  140. DEFINE_RES_IRQ(BXTWC_THRM0_IRQ),
  141. DEFINE_RES_IRQ(BXTWC_THRM1_IRQ),
  142. DEFINE_RES_IRQ(BXTWC_THRM2_IRQ),
  143. };
  144. static struct resource bcu_resources[] = {
  145. DEFINE_RES_IRQ_NAMED(BXTWC_BCU_IRQ, "BCU"),
  146. };
  147. static struct mfd_cell bxt_wc_dev[] = {
  148. {
  149. .name = "bxt_wcove_gpadc",
  150. .num_resources = ARRAY_SIZE(adc_resources),
  151. .resources = adc_resources,
  152. },
  153. {
  154. .name = "bxt_wcove_thermal",
  155. .num_resources = ARRAY_SIZE(thermal_resources),
  156. .resources = thermal_resources,
  157. },
  158. {
  159. .name = "bxt_wcove_usbc",
  160. .num_resources = ARRAY_SIZE(usbc_resources),
  161. .resources = usbc_resources,
  162. },
  163. {
  164. .name = "bxt_wcove_ext_charger",
  165. .num_resources = ARRAY_SIZE(charger_resources),
  166. .resources = charger_resources,
  167. },
  168. {
  169. .name = "bxt_wcove_bcu",
  170. .num_resources = ARRAY_SIZE(bcu_resources),
  171. .resources = bcu_resources,
  172. },
  173. {
  174. .name = "bxt_wcove_gpio",
  175. .num_resources = ARRAY_SIZE(gpio_resources),
  176. .resources = gpio_resources,
  177. },
  178. {
  179. .name = "bxt_wcove_region",
  180. },
  181. };
  182. static int regmap_ipc_byte_reg_read(void *context, unsigned int reg,
  183. unsigned int *val)
  184. {
  185. int ret;
  186. int i2c_addr;
  187. u8 ipc_in[2];
  188. u8 ipc_out[4];
  189. struct intel_soc_pmic *pmic = context;
  190. if (reg & REG_ADDR_MASK)
  191. i2c_addr = (reg & REG_ADDR_MASK) >> REG_ADDR_SHIFT;
  192. else {
  193. i2c_addr = BXTWC_DEVICE1_ADDR;
  194. if (!i2c_addr) {
  195. dev_err(pmic->dev, "I2C address not set\n");
  196. return -EINVAL;
  197. }
  198. }
  199. reg &= REG_OFFSET_MASK;
  200. ipc_in[0] = reg;
  201. ipc_in[1] = i2c_addr;
  202. ret = intel_pmc_ipc_command(PMC_IPC_PMIC_ACCESS,
  203. PMC_IPC_PMIC_ACCESS_READ,
  204. ipc_in, sizeof(ipc_in), (u32 *)ipc_out, 1);
  205. if (ret) {
  206. dev_err(pmic->dev, "Failed to read from PMIC\n");
  207. return ret;
  208. }
  209. *val = ipc_out[0];
  210. return 0;
  211. }
  212. static int regmap_ipc_byte_reg_write(void *context, unsigned int reg,
  213. unsigned int val)
  214. {
  215. int ret;
  216. int i2c_addr;
  217. u8 ipc_in[3];
  218. struct intel_soc_pmic *pmic = context;
  219. if (reg & REG_ADDR_MASK)
  220. i2c_addr = (reg & REG_ADDR_MASK) >> REG_ADDR_SHIFT;
  221. else {
  222. i2c_addr = BXTWC_DEVICE1_ADDR;
  223. if (!i2c_addr) {
  224. dev_err(pmic->dev, "I2C address not set\n");
  225. return -EINVAL;
  226. }
  227. }
  228. reg &= REG_OFFSET_MASK;
  229. ipc_in[0] = reg;
  230. ipc_in[1] = i2c_addr;
  231. ipc_in[2] = val;
  232. ret = intel_pmc_ipc_command(PMC_IPC_PMIC_ACCESS,
  233. PMC_IPC_PMIC_ACCESS_WRITE,
  234. ipc_in, sizeof(ipc_in), NULL, 0);
  235. if (ret) {
  236. dev_err(pmic->dev, "Failed to write to PMIC\n");
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. /* sysfs interfaces to r/w PMIC registers, required by initial script */
  242. static unsigned long bxtwc_reg_addr;
  243. static ssize_t bxtwc_reg_show(struct device *dev,
  244. struct device_attribute *attr, char *buf)
  245. {
  246. return sprintf(buf, "0x%lx\n", bxtwc_reg_addr);
  247. }
  248. static ssize_t bxtwc_reg_store(struct device *dev,
  249. struct device_attribute *attr, const char *buf, size_t count)
  250. {
  251. if (kstrtoul(buf, 0, &bxtwc_reg_addr)) {
  252. dev_err(dev, "Invalid register address\n");
  253. return -EINVAL;
  254. }
  255. return (ssize_t)count;
  256. }
  257. static ssize_t bxtwc_val_show(struct device *dev,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. int ret;
  261. unsigned int val;
  262. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  263. ret = regmap_read(pmic->regmap, bxtwc_reg_addr, &val);
  264. if (ret < 0) {
  265. dev_err(dev, "Failed to read 0x%lx\n", bxtwc_reg_addr);
  266. return -EIO;
  267. }
  268. return sprintf(buf, "0x%02x\n", val);
  269. }
  270. static ssize_t bxtwc_val_store(struct device *dev,
  271. struct device_attribute *attr, const char *buf, size_t count)
  272. {
  273. int ret;
  274. unsigned int val;
  275. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  276. ret = kstrtouint(buf, 0, &val);
  277. if (ret)
  278. return ret;
  279. ret = regmap_write(pmic->regmap, bxtwc_reg_addr, val);
  280. if (ret) {
  281. dev_err(dev, "Failed to write value 0x%02x to address 0x%lx",
  282. val, bxtwc_reg_addr);
  283. return -EIO;
  284. }
  285. return count;
  286. }
  287. static DEVICE_ATTR(addr, S_IWUSR | S_IRUSR, bxtwc_reg_show, bxtwc_reg_store);
  288. static DEVICE_ATTR(val, S_IWUSR | S_IRUSR, bxtwc_val_show, bxtwc_val_store);
  289. static struct attribute *bxtwc_attrs[] = {
  290. &dev_attr_addr.attr,
  291. &dev_attr_val.attr,
  292. NULL
  293. };
  294. static const struct attribute_group bxtwc_group = {
  295. .attrs = bxtwc_attrs,
  296. };
  297. static const struct regmap_config bxtwc_regmap_config = {
  298. .reg_bits = 16,
  299. .val_bits = 8,
  300. .reg_write = regmap_ipc_byte_reg_write,
  301. .reg_read = regmap_ipc_byte_reg_read,
  302. };
  303. static int bxtwc_probe(struct platform_device *pdev)
  304. {
  305. int ret;
  306. acpi_handle handle;
  307. acpi_status status;
  308. unsigned long long hrv;
  309. struct intel_soc_pmic *pmic;
  310. handle = ACPI_HANDLE(&pdev->dev);
  311. status = acpi_evaluate_integer(handle, "_HRV", NULL, &hrv);
  312. if (ACPI_FAILURE(status)) {
  313. dev_err(&pdev->dev, "Failed to get PMIC hardware revision\n");
  314. return -ENODEV;
  315. }
  316. if (hrv != BROXTON_PMIC_WC_HRV) {
  317. dev_err(&pdev->dev, "Invalid PMIC hardware revision: %llu\n",
  318. hrv);
  319. return -ENODEV;
  320. }
  321. pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
  322. if (!pmic)
  323. return -ENOMEM;
  324. ret = platform_get_irq(pdev, 0);
  325. if (ret < 0) {
  326. dev_err(&pdev->dev, "Invalid IRQ\n");
  327. return ret;
  328. }
  329. pmic->irq = ret;
  330. dev_set_drvdata(&pdev->dev, pmic);
  331. pmic->dev = &pdev->dev;
  332. pmic->regmap = devm_regmap_init(&pdev->dev, NULL, pmic,
  333. &bxtwc_regmap_config);
  334. if (IS_ERR(pmic->regmap)) {
  335. ret = PTR_ERR(pmic->regmap);
  336. dev_err(&pdev->dev, "Failed to initialise regmap: %d\n", ret);
  337. return ret;
  338. }
  339. ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
  340. IRQF_ONESHOT | IRQF_SHARED,
  341. 0, &bxtwc_regmap_irq_chip,
  342. &pmic->irq_chip_data);
  343. if (ret) {
  344. dev_err(&pdev->dev, "Failed to add IRQ chip\n");
  345. return ret;
  346. }
  347. ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
  348. IRQF_ONESHOT | IRQF_SHARED,
  349. 0, &bxtwc_regmap_irq_chip_level2,
  350. &pmic->irq_chip_data_level2);
  351. if (ret) {
  352. dev_err(&pdev->dev, "Failed to add secondary IRQ chip\n");
  353. goto err_irq_chip_level2;
  354. }
  355. ret = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE, bxt_wc_dev,
  356. ARRAY_SIZE(bxt_wc_dev), NULL, 0,
  357. NULL);
  358. if (ret) {
  359. dev_err(&pdev->dev, "Failed to add devices\n");
  360. goto err_mfd;
  361. }
  362. ret = sysfs_create_group(&pdev->dev.kobj, &bxtwc_group);
  363. if (ret) {
  364. dev_err(&pdev->dev, "Failed to create sysfs group %d\n", ret);
  365. goto err_sysfs;
  366. }
  367. /*
  368. * There is known hw bug. Upon reset BIT 5 of register
  369. * BXTWC_CHGR_LVL1_IRQ is 0 which is the expected value. However,
  370. * later it's set to 1(masked) automatically by hardware. So we
  371. * have the software workaround here to unmaksed it in order to let
  372. * charger interrutp work.
  373. */
  374. regmap_update_bits(pmic->regmap, BXTWC_MIRQLVL1,
  375. BXTWC_MIRQLVL1_MCHGR, 0);
  376. return 0;
  377. err_sysfs:
  378. mfd_remove_devices(&pdev->dev);
  379. err_mfd:
  380. regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data_level2);
  381. err_irq_chip_level2:
  382. regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
  383. return ret;
  384. }
  385. static int bxtwc_remove(struct platform_device *pdev)
  386. {
  387. struct intel_soc_pmic *pmic = dev_get_drvdata(&pdev->dev);
  388. sysfs_remove_group(&pdev->dev.kobj, &bxtwc_group);
  389. mfd_remove_devices(&pdev->dev);
  390. regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
  391. regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data_level2);
  392. return 0;
  393. }
  394. static void bxtwc_shutdown(struct platform_device *pdev)
  395. {
  396. struct intel_soc_pmic *pmic = dev_get_drvdata(&pdev->dev);
  397. disable_irq(pmic->irq);
  398. }
  399. #ifdef CONFIG_PM_SLEEP
  400. static int bxtwc_suspend(struct device *dev)
  401. {
  402. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  403. disable_irq(pmic->irq);
  404. return 0;
  405. }
  406. static int bxtwc_resume(struct device *dev)
  407. {
  408. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  409. enable_irq(pmic->irq);
  410. return 0;
  411. }
  412. #endif
  413. static SIMPLE_DEV_PM_OPS(bxtwc_pm_ops, bxtwc_suspend, bxtwc_resume);
  414. static const struct acpi_device_id bxtwc_acpi_ids[] = {
  415. { "INT34D3", },
  416. { }
  417. };
  418. MODULE_DEVICE_TABLE(acpi, pmic_acpi_ids);
  419. static struct platform_driver bxtwc_driver = {
  420. .probe = bxtwc_probe,
  421. .remove = bxtwc_remove,
  422. .shutdown = bxtwc_shutdown,
  423. .driver = {
  424. .name = "BXTWC PMIC",
  425. .pm = &bxtwc_pm_ops,
  426. .acpi_match_table = ACPI_PTR(bxtwc_acpi_ids),
  427. },
  428. };
  429. module_platform_driver(bxtwc_driver);
  430. MODULE_LICENSE("GPL v2");
  431. MODULE_AUTHOR("Qipeng Zha<qipeng.zha@intel.com>");