intel_msic.c 12 KB

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