intel_msic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Driver for Intel MSIC
  3. *
  4. * Copyright (C) 2011, Intel Corporation
  5. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/gpio.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/mfd/core.h>
  16. #include <linux/mfd/intel_msic.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <asm/intel_scu_ipc.h>
  20. #define MSIC_VENDOR(id) ((id >> 6) & 3)
  21. #define MSIC_VERSION(id) (id & 0x3f)
  22. #define MSIC_MAJOR(id) ('A' + ((id >> 3) & 7))
  23. #define MSIC_MINOR(id) (id & 7)
  24. /*
  25. * MSIC interrupt tree is readable from SRAM at INTEL_MSIC_IRQ_PHYS_BASE.
  26. * Since IRQ block starts from address 0x002 we need to subtract that from
  27. * the actual IRQ status register address.
  28. */
  29. #define MSIC_IRQ_STATUS(x) (INTEL_MSIC_IRQ_PHYS_BASE + ((x) - 2))
  30. #define MSIC_IRQ_STATUS_ACCDET MSIC_IRQ_STATUS(INTEL_MSIC_ACCDET)
  31. /*
  32. * The SCU hardware has limitation of 16 bytes per read/write buffer on
  33. * Medfield.
  34. */
  35. #define SCU_IPC_RWBUF_LIMIT 16
  36. /**
  37. * struct intel_msic - an MSIC MFD instance
  38. * @pdev: pointer to the platform device
  39. * @vendor: vendor ID
  40. * @version: chip version
  41. * @irq_base: base address of the mapped MSIC SRAM interrupt tree
  42. */
  43. struct intel_msic {
  44. struct platform_device *pdev;
  45. unsigned vendor;
  46. unsigned version;
  47. void __iomem *irq_base;
  48. };
  49. static struct resource msic_touch_resources[] = {
  50. {
  51. .flags = IORESOURCE_IRQ,
  52. },
  53. };
  54. static struct resource msic_adc_resources[] = {
  55. {
  56. .flags = IORESOURCE_IRQ,
  57. },
  58. };
  59. static struct resource msic_battery_resources[] = {
  60. {
  61. .flags = IORESOURCE_IRQ,
  62. },
  63. };
  64. static struct resource msic_gpio_resources[] = {
  65. {
  66. .flags = IORESOURCE_IRQ,
  67. },
  68. };
  69. static struct resource msic_audio_resources[] = {
  70. {
  71. .name = "IRQ",
  72. .flags = IORESOURCE_IRQ,
  73. },
  74. /*
  75. * We will pass IRQ_BASE to the driver now but this can be removed
  76. * when/if the driver starts to use intel_msic_irq_read().
  77. */
  78. {
  79. .name = "IRQ_BASE",
  80. .flags = IORESOURCE_MEM,
  81. .start = MSIC_IRQ_STATUS_ACCDET,
  82. .end = MSIC_IRQ_STATUS_ACCDET,
  83. },
  84. };
  85. static struct resource msic_hdmi_resources[] = {
  86. {
  87. .flags = IORESOURCE_IRQ,
  88. },
  89. };
  90. static struct resource msic_thermal_resources[] = {
  91. {
  92. .flags = IORESOURCE_IRQ,
  93. },
  94. };
  95. static struct resource msic_power_btn_resources[] = {
  96. {
  97. .flags = IORESOURCE_IRQ,
  98. },
  99. };
  100. static struct resource msic_ocd_resources[] = {
  101. {
  102. .flags = IORESOURCE_IRQ,
  103. },
  104. };
  105. /*
  106. * Devices that are part of the MSIC and are available via firmware
  107. * populated SFI DEVS table.
  108. */
  109. static struct mfd_cell msic_devs[] = {
  110. [INTEL_MSIC_BLOCK_TOUCH] = {
  111. .name = "msic_touch",
  112. .num_resources = ARRAY_SIZE(msic_touch_resources),
  113. .resources = msic_touch_resources,
  114. },
  115. [INTEL_MSIC_BLOCK_ADC] = {
  116. .name = "msic_adc",
  117. .num_resources = ARRAY_SIZE(msic_adc_resources),
  118. .resources = msic_adc_resources,
  119. },
  120. [INTEL_MSIC_BLOCK_BATTERY] = {
  121. .name = "msic_battery",
  122. .num_resources = ARRAY_SIZE(msic_battery_resources),
  123. .resources = msic_battery_resources,
  124. },
  125. [INTEL_MSIC_BLOCK_GPIO] = {
  126. .name = "msic_gpio",
  127. .num_resources = ARRAY_SIZE(msic_gpio_resources),
  128. .resources = msic_gpio_resources,
  129. },
  130. [INTEL_MSIC_BLOCK_AUDIO] = {
  131. .name = "msic_audio",
  132. .num_resources = ARRAY_SIZE(msic_audio_resources),
  133. .resources = msic_audio_resources,
  134. },
  135. [INTEL_MSIC_BLOCK_HDMI] = {
  136. .name = "msic_hdmi",
  137. .num_resources = ARRAY_SIZE(msic_hdmi_resources),
  138. .resources = msic_hdmi_resources,
  139. },
  140. [INTEL_MSIC_BLOCK_THERMAL] = {
  141. .name = "msic_thermal",
  142. .num_resources = ARRAY_SIZE(msic_thermal_resources),
  143. .resources = msic_thermal_resources,
  144. },
  145. [INTEL_MSIC_BLOCK_POWER_BTN] = {
  146. .name = "msic_power_btn",
  147. .num_resources = ARRAY_SIZE(msic_power_btn_resources),
  148. .resources = msic_power_btn_resources,
  149. },
  150. [INTEL_MSIC_BLOCK_OCD] = {
  151. .name = "msic_ocd",
  152. .num_resources = ARRAY_SIZE(msic_ocd_resources),
  153. .resources = msic_ocd_resources,
  154. },
  155. };
  156. /*
  157. * Other MSIC related devices which are not directly available via SFI DEVS
  158. * table. These can be pseudo devices, regulators etc. which are needed for
  159. * different purposes.
  160. *
  161. * These devices appear only after the MSIC driver itself is initialized so
  162. * we can guarantee that the SCU IPC interface is ready.
  163. */
  164. static const struct mfd_cell msic_other_devs[] = {
  165. /* Audio codec in the MSIC */
  166. {
  167. .id = -1,
  168. .name = "sn95031",
  169. },
  170. };
  171. /**
  172. * intel_msic_reg_read - read a single MSIC register
  173. * @reg: register to read
  174. * @val: register value is placed here
  175. *
  176. * Read a single register from MSIC. Returns %0 on success and negative
  177. * errno in case of failure.
  178. *
  179. * Function may sleep.
  180. */
  181. int intel_msic_reg_read(unsigned short reg, u8 *val)
  182. {
  183. return intel_scu_ipc_ioread8(reg, val);
  184. }
  185. EXPORT_SYMBOL_GPL(intel_msic_reg_read);
  186. /**
  187. * intel_msic_reg_write - write a single MSIC register
  188. * @reg: register to write
  189. * @val: value to write to that register
  190. *
  191. * Write a single MSIC register. Returns 0 on success and negative
  192. * errno in case of failure.
  193. *
  194. * Function may sleep.
  195. */
  196. int intel_msic_reg_write(unsigned short reg, u8 val)
  197. {
  198. return intel_scu_ipc_iowrite8(reg, val);
  199. }
  200. EXPORT_SYMBOL_GPL(intel_msic_reg_write);
  201. /**
  202. * intel_msic_reg_update - update a single MSIC register
  203. * @reg: register to update
  204. * @val: value to write to the register
  205. * @mask: specifies which of the bits are updated (%0 = don't update,
  206. * %1 = update)
  207. *
  208. * Perform an update to a register @reg. @mask is used to specify which
  209. * bits are updated. Returns %0 in case of success and negative errno in
  210. * case of failure.
  211. *
  212. * Function may sleep.
  213. */
  214. int intel_msic_reg_update(unsigned short reg, u8 val, u8 mask)
  215. {
  216. return intel_scu_ipc_update_register(reg, val, mask);
  217. }
  218. EXPORT_SYMBOL_GPL(intel_msic_reg_update);
  219. /**
  220. * intel_msic_bulk_read - read an array of registers
  221. * @reg: array of register addresses to read
  222. * @buf: array where the read values are placed
  223. * @count: number of registers to read
  224. *
  225. * Function reads @count registers from the MSIC using addresses passed in
  226. * @reg. Read values are placed in @buf. Reads are performed atomically
  227. * wrt. MSIC.
  228. *
  229. * Returns %0 in case of success and negative errno in case of failure.
  230. *
  231. * Function may sleep.
  232. */
  233. int intel_msic_bulk_read(unsigned short *reg, u8 *buf, size_t count)
  234. {
  235. if (WARN_ON(count > SCU_IPC_RWBUF_LIMIT))
  236. return -EINVAL;
  237. return intel_scu_ipc_readv(reg, buf, count);
  238. }
  239. EXPORT_SYMBOL_GPL(intel_msic_bulk_read);
  240. /**
  241. * intel_msic_bulk_write - write an array of values to the MSIC registers
  242. * @reg: array of registers to write
  243. * @buf: values to write to each register
  244. * @count: number of registers to write
  245. *
  246. * Function writes @count registers in @buf to MSIC. Writes are performed
  247. * atomically wrt MSIC. Returns %0 in case of success and negative errno in
  248. * case of failure.
  249. *
  250. * Function may sleep.
  251. */
  252. int intel_msic_bulk_write(unsigned short *reg, u8 *buf, size_t count)
  253. {
  254. if (WARN_ON(count > SCU_IPC_RWBUF_LIMIT))
  255. return -EINVAL;
  256. return intel_scu_ipc_writev(reg, buf, count);
  257. }
  258. EXPORT_SYMBOL_GPL(intel_msic_bulk_write);
  259. /**
  260. * intel_msic_irq_read - read a register from an MSIC interrupt tree
  261. * @msic: MSIC instance
  262. * @reg: interrupt register (between %INTEL_MSIC_IRQLVL1 and
  263. * %INTEL_MSIC_RESETIRQ2)
  264. * @val: value of the register is placed here
  265. *
  266. * This function can be used by an MSIC subdevice interrupt handler to read
  267. * a register value from the MSIC interrupt tree. In this way subdevice
  268. * drivers don't have to map in the interrupt tree themselves but can just
  269. * call this function instead.
  270. *
  271. * Function doesn't sleep and is callable from interrupt context.
  272. *
  273. * Returns %-EINVAL if @reg is outside of the allowed register region.
  274. */
  275. int intel_msic_irq_read(struct intel_msic *msic, unsigned short reg, u8 *val)
  276. {
  277. if (WARN_ON(reg < INTEL_MSIC_IRQLVL1 || reg > INTEL_MSIC_RESETIRQ2))
  278. return -EINVAL;
  279. *val = readb(msic->irq_base + (reg - INTEL_MSIC_IRQLVL1));
  280. return 0;
  281. }
  282. EXPORT_SYMBOL_GPL(intel_msic_irq_read);
  283. static int intel_msic_init_devices(struct intel_msic *msic)
  284. {
  285. struct platform_device *pdev = msic->pdev;
  286. struct intel_msic_platform_data *pdata = dev_get_platdata(&pdev->dev);
  287. int ret, i;
  288. if (pdata->gpio) {
  289. struct mfd_cell *cell = &msic_devs[INTEL_MSIC_BLOCK_GPIO];
  290. cell->platform_data = pdata->gpio;
  291. cell->pdata_size = sizeof(*pdata->gpio);
  292. }
  293. if (pdata->ocd) {
  294. unsigned gpio = pdata->ocd->gpio;
  295. ret = devm_gpio_request_one(&pdev->dev, gpio,
  296. GPIOF_IN, "ocd_gpio");
  297. if (ret) {
  298. dev_err(&pdev->dev, "failed to register OCD GPIO\n");
  299. return ret;
  300. }
  301. ret = gpio_to_irq(gpio);
  302. if (ret < 0) {
  303. dev_err(&pdev->dev, "no IRQ number for OCD GPIO\n");
  304. return ret;
  305. }
  306. /* Update the IRQ number for the OCD */
  307. pdata->irq[INTEL_MSIC_BLOCK_OCD] = ret;
  308. }
  309. for (i = 0; i < ARRAY_SIZE(msic_devs); i++) {
  310. if (!pdata->irq[i])
  311. continue;
  312. ret = mfd_add_devices(&pdev->dev, -1, &msic_devs[i], 1, NULL,
  313. pdata->irq[i], NULL);
  314. if (ret)
  315. goto fail;
  316. }
  317. ret = mfd_add_devices(&pdev->dev, 0, msic_other_devs,
  318. ARRAY_SIZE(msic_other_devs), NULL, 0, NULL);
  319. if (ret)
  320. goto fail;
  321. return 0;
  322. fail:
  323. mfd_remove_devices(&pdev->dev);
  324. return ret;
  325. }
  326. static void intel_msic_remove_devices(struct intel_msic *msic)
  327. {
  328. struct platform_device *pdev = msic->pdev;
  329. mfd_remove_devices(&pdev->dev);
  330. }
  331. static int intel_msic_probe(struct platform_device *pdev)
  332. {
  333. struct intel_msic_platform_data *pdata = dev_get_platdata(&pdev->dev);
  334. struct intel_msic *msic;
  335. struct resource *res;
  336. u8 id0, id1;
  337. int ret;
  338. if (!pdata) {
  339. dev_err(&pdev->dev, "no platform data passed\n");
  340. return -EINVAL;
  341. }
  342. /* First validate that we have an MSIC in place */
  343. ret = intel_scu_ipc_ioread8(INTEL_MSIC_ID0, &id0);
  344. if (ret) {
  345. dev_err(&pdev->dev, "failed to identify the MSIC chip (ID0)\n");
  346. return -ENXIO;
  347. }
  348. ret = intel_scu_ipc_ioread8(INTEL_MSIC_ID1, &id1);
  349. if (ret) {
  350. dev_err(&pdev->dev, "failed to identify the MSIC chip (ID1)\n");
  351. return -ENXIO;
  352. }
  353. if (MSIC_VENDOR(id0) != MSIC_VENDOR(id1)) {
  354. dev_err(&pdev->dev, "invalid vendor ID: %x, %x\n", id0, id1);
  355. return -ENXIO;
  356. }
  357. msic = devm_kzalloc(&pdev->dev, sizeof(*msic), GFP_KERNEL);
  358. if (!msic)
  359. return -ENOMEM;
  360. msic->vendor = MSIC_VENDOR(id0);
  361. msic->version = MSIC_VERSION(id0);
  362. msic->pdev = pdev;
  363. /*
  364. * Map in the MSIC interrupt tree area in SRAM. This is exposed to
  365. * the clients via intel_msic_irq_read().
  366. */
  367. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  368. msic->irq_base = devm_ioremap_resource(&pdev->dev, res);
  369. if (IS_ERR(msic->irq_base))
  370. return PTR_ERR(msic->irq_base);
  371. platform_set_drvdata(pdev, msic);
  372. ret = intel_msic_init_devices(msic);
  373. if (ret) {
  374. dev_err(&pdev->dev, "failed to initialize MSIC devices\n");
  375. return ret;
  376. }
  377. dev_info(&pdev->dev, "Intel MSIC version %c%d (vendor %#x)\n",
  378. MSIC_MAJOR(msic->version), MSIC_MINOR(msic->version),
  379. msic->vendor);
  380. return 0;
  381. }
  382. static int intel_msic_remove(struct platform_device *pdev)
  383. {
  384. struct intel_msic *msic = platform_get_drvdata(pdev);
  385. intel_msic_remove_devices(msic);
  386. return 0;
  387. }
  388. static struct platform_driver intel_msic_driver = {
  389. .probe = intel_msic_probe,
  390. .remove = intel_msic_remove,
  391. .driver = {
  392. .name = "intel_msic",
  393. },
  394. };
  395. module_platform_driver(intel_msic_driver);
  396. MODULE_DESCRIPTION("Driver for Intel MSIC");
  397. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  398. MODULE_LICENSE("GPL");