sta2x11-mfd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (c) 2009-2011 Wind River Systems, Inc.
  3. * Copyright (c) 2011 ST Microelectronics (Alessandro Rubini)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/errno.h>
  23. #include <linux/device.h>
  24. #include <linux/slab.h>
  25. #include <linux/list.h>
  26. #include <linux/io.h>
  27. #include <linux/ioport.h>
  28. #include <linux/pci.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mfd/core.h>
  33. #include <linux/mfd/sta2x11-mfd.h>
  34. #include <asm/sta2x11.h>
  35. /* This describes STA2X11 MFD chip for us, we may have several */
  36. struct sta2x11_mfd {
  37. struct sta2x11_instance *instance;
  38. spinlock_t lock;
  39. struct list_head list;
  40. void __iomem *sctl_regs;
  41. void __iomem *apbreg_regs;
  42. };
  43. static LIST_HEAD(sta2x11_mfd_list);
  44. /* Three functions to act on the list */
  45. static struct sta2x11_mfd *sta2x11_mfd_find(struct pci_dev *pdev)
  46. {
  47. struct sta2x11_instance *instance;
  48. struct sta2x11_mfd *mfd;
  49. if (!pdev && !list_empty(&sta2x11_mfd_list)) {
  50. pr_warning("%s: Unspecified device, "
  51. "using first instance\n", __func__);
  52. return list_entry(sta2x11_mfd_list.next,
  53. struct sta2x11_mfd, list);
  54. }
  55. instance = sta2x11_get_instance(pdev);
  56. if (!instance)
  57. return NULL;
  58. list_for_each_entry(mfd, &sta2x11_mfd_list, list) {
  59. if (mfd->instance == instance)
  60. return mfd;
  61. }
  62. return NULL;
  63. }
  64. static int __devinit sta2x11_mfd_add(struct pci_dev *pdev, gfp_t flags)
  65. {
  66. struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
  67. struct sta2x11_instance *instance;
  68. if (mfd)
  69. return -EBUSY;
  70. instance = sta2x11_get_instance(pdev);
  71. if (!instance)
  72. return -EINVAL;
  73. mfd = kzalloc(sizeof(*mfd), flags);
  74. if (!mfd)
  75. return -ENOMEM;
  76. INIT_LIST_HEAD(&mfd->list);
  77. spin_lock_init(&mfd->lock);
  78. mfd->instance = instance;
  79. list_add(&mfd->list, &sta2x11_mfd_list);
  80. return 0;
  81. }
  82. static int __devexit mfd_remove(struct pci_dev *pdev)
  83. {
  84. struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
  85. if (!mfd)
  86. return -ENODEV;
  87. list_del(&mfd->list);
  88. kfree(mfd);
  89. return 0;
  90. }
  91. /* These two functions are exported and are not expected to fail */
  92. u32 sta2x11_sctl_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val)
  93. {
  94. struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
  95. u32 r;
  96. unsigned long flags;
  97. if (!mfd) {
  98. dev_warn(&pdev->dev, ": can't access sctl regs\n");
  99. return 0;
  100. }
  101. if (!mfd->sctl_regs) {
  102. dev_warn(&pdev->dev, ": system ctl not initialized\n");
  103. return 0;
  104. }
  105. spin_lock_irqsave(&mfd->lock, flags);
  106. r = readl(mfd->sctl_regs + reg);
  107. r &= ~mask;
  108. r |= val;
  109. if (mask)
  110. writel(r, mfd->sctl_regs + reg);
  111. spin_unlock_irqrestore(&mfd->lock, flags);
  112. return r;
  113. }
  114. EXPORT_SYMBOL(sta2x11_sctl_mask);
  115. u32 sta2x11_apbreg_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val)
  116. {
  117. struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
  118. u32 r;
  119. unsigned long flags;
  120. if (!mfd) {
  121. dev_warn(&pdev->dev, ": can't access apb regs\n");
  122. return 0;
  123. }
  124. if (!mfd->apbreg_regs) {
  125. dev_warn(&pdev->dev, ": apb bridge not initialized\n");
  126. return 0;
  127. }
  128. spin_lock_irqsave(&mfd->lock, flags);
  129. r = readl(mfd->apbreg_regs + reg);
  130. r &= ~mask;
  131. r |= val;
  132. if (mask)
  133. writel(r, mfd->apbreg_regs + reg);
  134. spin_unlock_irqrestore(&mfd->lock, flags);
  135. return r;
  136. }
  137. EXPORT_SYMBOL(sta2x11_apbreg_mask);
  138. /* Two debugfs files, for our registers (FIXME: one instance only) */
  139. #define REG(regname) {.name = #regname, .offset = SCTL_ ## regname}
  140. static struct debugfs_reg32 sta2x11_sctl_regs[] = {
  141. REG(SCCTL), REG(ARMCFG), REG(SCPLLCTL), REG(SCPLLFCTRL),
  142. REG(SCRESFRACT), REG(SCRESCTRL1), REG(SCRESXTRL2), REG(SCPEREN0),
  143. REG(SCPEREN1), REG(SCPEREN2), REG(SCGRST), REG(SCPCIPMCR1),
  144. REG(SCPCIPMCR2), REG(SCPCIPMSR1), REG(SCPCIPMSR2), REG(SCPCIPMSR3),
  145. REG(SCINTREN), REG(SCRISR), REG(SCCLKSTAT0), REG(SCCLKSTAT1),
  146. REG(SCCLKSTAT2), REG(SCRSTSTA),
  147. };
  148. #undef REG
  149. static struct debugfs_regset32 sctl_regset = {
  150. .regs = sta2x11_sctl_regs,
  151. .nregs = ARRAY_SIZE(sta2x11_sctl_regs),
  152. };
  153. #define REG(regname) {.name = #regname, .offset = regname}
  154. static struct debugfs_reg32 sta2x11_apbreg_regs[] = {
  155. REG(APBREG_BSR), REG(APBREG_PAER), REG(APBREG_PWAC), REG(APBREG_PRAC),
  156. REG(APBREG_PCG), REG(APBREG_PUR), REG(APBREG_EMU_PCG),
  157. };
  158. #undef REG
  159. static struct debugfs_regset32 apbreg_regset = {
  160. .regs = sta2x11_apbreg_regs,
  161. .nregs = ARRAY_SIZE(sta2x11_apbreg_regs),
  162. };
  163. static struct dentry *sta2x11_sctl_debugfs;
  164. static struct dentry *sta2x11_apbreg_debugfs;
  165. /* Probe for the two platform devices */
  166. static int sta2x11_sctl_probe(struct platform_device *dev)
  167. {
  168. struct pci_dev **pdev;
  169. struct sta2x11_mfd *mfd;
  170. struct resource *res;
  171. pdev = dev->dev.platform_data;
  172. mfd = sta2x11_mfd_find(*pdev);
  173. if (!mfd)
  174. return -ENODEV;
  175. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  176. if (!res)
  177. return -ENOMEM;
  178. if (!request_mem_region(res->start, resource_size(res),
  179. "sta2x11-sctl"))
  180. return -EBUSY;
  181. mfd->sctl_regs = ioremap(res->start, resource_size(res));
  182. if (!mfd->sctl_regs) {
  183. release_mem_region(res->start, resource_size(res));
  184. return -ENOMEM;
  185. }
  186. sctl_regset.base = mfd->sctl_regs;
  187. sta2x11_sctl_debugfs = debugfs_create_regset32("sta2x11-sctl",
  188. S_IFREG | S_IRUGO,
  189. NULL, &sctl_regset);
  190. return 0;
  191. }
  192. static int sta2x11_apbreg_probe(struct platform_device *dev)
  193. {
  194. struct pci_dev **pdev;
  195. struct sta2x11_mfd *mfd;
  196. struct resource *res;
  197. pdev = dev->dev.platform_data;
  198. dev_dbg(&dev->dev, "%s: pdata is %p\n", __func__, pdev);
  199. dev_dbg(&dev->dev, "%s: *pdata is %p\n", __func__, *pdev);
  200. mfd = sta2x11_mfd_find(*pdev);
  201. if (!mfd)
  202. return -ENODEV;
  203. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  204. if (!res)
  205. return -ENOMEM;
  206. if (!request_mem_region(res->start, resource_size(res),
  207. "sta2x11-apbreg"))
  208. return -EBUSY;
  209. mfd->apbreg_regs = ioremap(res->start, resource_size(res));
  210. if (!mfd->apbreg_regs) {
  211. release_mem_region(res->start, resource_size(res));
  212. return -ENOMEM;
  213. }
  214. dev_dbg(&dev->dev, "%s: regbase %p\n", __func__, mfd->apbreg_regs);
  215. apbreg_regset.base = mfd->apbreg_regs;
  216. sta2x11_apbreg_debugfs = debugfs_create_regset32("sta2x11-apbreg",
  217. S_IFREG | S_IRUGO,
  218. NULL, &apbreg_regset);
  219. return 0;
  220. }
  221. /* The two platform drivers */
  222. static struct platform_driver sta2x11_sctl_platform_driver = {
  223. .driver = {
  224. .name = "sta2x11-sctl",
  225. .owner = THIS_MODULE,
  226. },
  227. .probe = sta2x11_sctl_probe,
  228. };
  229. static int __init sta2x11_sctl_init(void)
  230. {
  231. pr_info("%s\n", __func__);
  232. return platform_driver_register(&sta2x11_sctl_platform_driver);
  233. }
  234. static struct platform_driver sta2x11_platform_driver = {
  235. .driver = {
  236. .name = "sta2x11-apbreg",
  237. .owner = THIS_MODULE,
  238. },
  239. .probe = sta2x11_apbreg_probe,
  240. };
  241. static int __init sta2x11_apbreg_init(void)
  242. {
  243. pr_info("%s\n", __func__);
  244. return platform_driver_register(&sta2x11_platform_driver);
  245. }
  246. /*
  247. * What follows is the PCI device that hosts the above two pdevs.
  248. * Each logic block is 4kB and they are all consecutive: we use this info.
  249. */
  250. /* Bar 0 */
  251. enum bar0_cells {
  252. STA2X11_GPIO_0 = 0,
  253. STA2X11_GPIO_1,
  254. STA2X11_GPIO_2,
  255. STA2X11_GPIO_3,
  256. STA2X11_SCTL,
  257. STA2X11_SCR,
  258. STA2X11_TIME,
  259. };
  260. /* Bar 1 */
  261. enum bar1_cells {
  262. STA2X11_APBREG = 0,
  263. };
  264. #define CELL_4K(_name, _cell) { \
  265. .name = _name, \
  266. .start = _cell * 4096, .end = _cell * 4096 + 4095, \
  267. .flags = IORESOURCE_MEM, \
  268. }
  269. static const __devinitconst struct resource gpio_resources[] = {
  270. {
  271. .name = "sta2x11_gpio", /* 4 consecutive cells, 1 driver */
  272. .start = 0,
  273. .end = (4 * 4096) - 1,
  274. .flags = IORESOURCE_MEM,
  275. }
  276. };
  277. static const __devinitconst struct resource sctl_resources[] = {
  278. CELL_4K("sta2x11-sctl", STA2X11_SCTL),
  279. };
  280. static const __devinitconst struct resource scr_resources[] = {
  281. CELL_4K("sta2x11-scr", STA2X11_SCR),
  282. };
  283. static const __devinitconst struct resource time_resources[] = {
  284. CELL_4K("sta2x11-time", STA2X11_TIME),
  285. };
  286. static const __devinitconst struct resource apbreg_resources[] = {
  287. CELL_4K("sta2x11-apbreg", STA2X11_APBREG),
  288. };
  289. #define DEV(_name, _r) \
  290. { .name = _name, .num_resources = ARRAY_SIZE(_r), .resources = _r, }
  291. static __devinitdata struct mfd_cell sta2x11_mfd_bar0[] = {
  292. DEV("sta2x11-gpio", gpio_resources), /* offset 0: we add pdata later */
  293. DEV("sta2x11-sctl", sctl_resources),
  294. DEV("sta2x11-scr", scr_resources),
  295. DEV("sta2x11-time", time_resources),
  296. };
  297. static __devinitdata struct mfd_cell sta2x11_mfd_bar1[] = {
  298. DEV("sta2x11-apbreg", apbreg_resources),
  299. };
  300. static int sta2x11_mfd_suspend(struct pci_dev *pdev, pm_message_t state)
  301. {
  302. pci_save_state(pdev);
  303. pci_disable_device(pdev);
  304. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  305. return 0;
  306. }
  307. static int sta2x11_mfd_resume(struct pci_dev *pdev)
  308. {
  309. int err;
  310. pci_set_power_state(pdev, 0);
  311. err = pci_enable_device(pdev);
  312. if (err)
  313. return err;
  314. pci_restore_state(pdev);
  315. return 0;
  316. }
  317. static int __devinit sta2x11_mfd_probe(struct pci_dev *pdev,
  318. const struct pci_device_id *pci_id)
  319. {
  320. int err, i;
  321. struct sta2x11_gpio_pdata *gpio_data;
  322. dev_info(&pdev->dev, "%s\n", __func__);
  323. err = pci_enable_device(pdev);
  324. if (err) {
  325. dev_err(&pdev->dev, "Can't enable device.\n");
  326. return err;
  327. }
  328. err = pci_enable_msi(pdev);
  329. if (err)
  330. dev_info(&pdev->dev, "Enable msi failed\n");
  331. /* Read gpio config data as pci device's platform data */
  332. gpio_data = dev_get_platdata(&pdev->dev);
  333. if (!gpio_data)
  334. dev_warn(&pdev->dev, "no gpio configuration\n");
  335. dev_dbg(&pdev->dev, "%s, gpio_data = %p (%p)\n", __func__,
  336. gpio_data, &gpio_data);
  337. dev_dbg(&pdev->dev, "%s, pdev = %p (%p)\n", __func__,
  338. pdev, &pdev);
  339. /* platform data is the pci device for all of them */
  340. for (i = 0; i < ARRAY_SIZE(sta2x11_mfd_bar0); i++) {
  341. sta2x11_mfd_bar0[i].pdata_size = sizeof(pdev);
  342. sta2x11_mfd_bar0[i].platform_data = &pdev;
  343. }
  344. sta2x11_mfd_bar1[0].pdata_size = sizeof(pdev);
  345. sta2x11_mfd_bar1[0].platform_data = &pdev;
  346. /* Record this pdev before mfd_add_devices: their probe looks for it */
  347. sta2x11_mfd_add(pdev, GFP_ATOMIC);
  348. err = mfd_add_devices(&pdev->dev, -1,
  349. sta2x11_mfd_bar0,
  350. ARRAY_SIZE(sta2x11_mfd_bar0),
  351. &pdev->resource[0],
  352. 0);
  353. if (err) {
  354. dev_err(&pdev->dev, "mfd_add_devices[0] failed: %d\n", err);
  355. goto err_disable;
  356. }
  357. err = mfd_add_devices(&pdev->dev, -1,
  358. sta2x11_mfd_bar1,
  359. ARRAY_SIZE(sta2x11_mfd_bar1),
  360. &pdev->resource[1],
  361. 0);
  362. if (err) {
  363. dev_err(&pdev->dev, "mfd_add_devices[1] failed: %d\n", err);
  364. goto err_disable;
  365. }
  366. return 0;
  367. err_disable:
  368. mfd_remove_devices(&pdev->dev);
  369. pci_disable_device(pdev);
  370. pci_disable_msi(pdev);
  371. return err;
  372. }
  373. static DEFINE_PCI_DEVICE_TABLE(sta2x11_mfd_tbl) = {
  374. {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_GPIO)},
  375. {0,},
  376. };
  377. static struct pci_driver sta2x11_mfd_driver = {
  378. .name = "sta2x11-mfd",
  379. .id_table = sta2x11_mfd_tbl,
  380. .probe = sta2x11_mfd_probe,
  381. .suspend = sta2x11_mfd_suspend,
  382. .resume = sta2x11_mfd_resume,
  383. };
  384. static int __init sta2x11_mfd_init(void)
  385. {
  386. pr_info("%s\n", __func__);
  387. return pci_register_driver(&sta2x11_mfd_driver);
  388. }
  389. /*
  390. * All of this must be ready before "normal" devices like MMCI appear.
  391. * But MFD (the pci device) can't be too early. The following choice
  392. * prepares platform drivers very early and probe the PCI device later,
  393. * but before other PCI devices.
  394. */
  395. subsys_initcall(sta2x11_apbreg_init);
  396. subsys_initcall(sta2x11_sctl_init);
  397. rootfs_initcall(sta2x11_mfd_init);
  398. MODULE_LICENSE("GPL v2");
  399. MODULE_AUTHOR("Wind River");
  400. MODULE_DESCRIPTION("STA2x11 mfd for GPIO, SCTL and APBREG");
  401. MODULE_DEVICE_TABLE(pci, sta2x11_mfd_tbl);