intel_quark_i2c_gpio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Intel Quark MFD PCI driver for I2C & GPIO
  3. *
  4. * Copyright(c) 2014 Intel Corporation.
  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. * Intel Quark PCI device for I2C and GPIO controller sharing the same
  16. * PCI function. This PCI driver will split the 2 devices into their
  17. * respective drivers.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/clkdev.h>
  24. #include <linux/clk-provider.h>
  25. #include <linux/dmi.h>
  26. #include <linux/platform_data/gpio-dwapb.h>
  27. #include <linux/platform_data/i2c-designware.h>
  28. /* PCI BAR for register base address */
  29. #define MFD_I2C_BAR 0
  30. #define MFD_GPIO_BAR 1
  31. /* The base GPIO number under GPIOLIB framework */
  32. #define INTEL_QUARK_MFD_GPIO_BASE 8
  33. /* The default number of South-Cluster GPIO on Quark. */
  34. #define INTEL_QUARK_MFD_NGPIO 8
  35. /* The DesignWare GPIO ports on Quark. */
  36. #define INTEL_QUARK_GPIO_NPORTS 1
  37. #define INTEL_QUARK_IORES_MEM 0
  38. #define INTEL_QUARK_IORES_IRQ 1
  39. #define INTEL_QUARK_I2C_CONTROLLER_CLK "i2c_designware.0"
  40. /* The Quark I2C controller source clock */
  41. #define INTEL_QUARK_I2C_CLK_HZ 33000000
  42. #define INTEL_QUARK_I2C_NCLK 1
  43. struct intel_quark_mfd {
  44. struct pci_dev *pdev;
  45. struct clk *i2c_clk;
  46. struct clk_lookup *i2c_clk_lookup;
  47. };
  48. struct i2c_mode_info {
  49. const char *name;
  50. unsigned int i2c_scl_freq;
  51. };
  52. static const struct i2c_mode_info platform_i2c_mode_info[] = {
  53. {
  54. .name = "Galileo",
  55. .i2c_scl_freq = 100000,
  56. },
  57. {
  58. .name = "GalileoGen2",
  59. .i2c_scl_freq = 400000,
  60. },
  61. {}
  62. };
  63. static struct resource intel_quark_i2c_res[] = {
  64. [INTEL_QUARK_IORES_MEM] = {
  65. .flags = IORESOURCE_MEM,
  66. },
  67. [INTEL_QUARK_IORES_IRQ] = {
  68. .flags = IORESOURCE_IRQ,
  69. },
  70. };
  71. static struct resource intel_quark_gpio_res[] = {
  72. [INTEL_QUARK_IORES_MEM] = {
  73. .flags = IORESOURCE_MEM,
  74. },
  75. };
  76. static struct mfd_cell intel_quark_mfd_cells[] = {
  77. {
  78. .id = MFD_I2C_BAR,
  79. .name = "i2c_designware",
  80. .num_resources = ARRAY_SIZE(intel_quark_i2c_res),
  81. .resources = intel_quark_i2c_res,
  82. .ignore_resource_conflicts = true,
  83. },
  84. {
  85. .id = MFD_GPIO_BAR,
  86. .name = "gpio-dwapb",
  87. .num_resources = ARRAY_SIZE(intel_quark_gpio_res),
  88. .resources = intel_quark_gpio_res,
  89. .ignore_resource_conflicts = true,
  90. },
  91. };
  92. static const struct pci_device_id intel_quark_mfd_ids[] = {
  93. { PCI_VDEVICE(INTEL, 0x0934), },
  94. {},
  95. };
  96. MODULE_DEVICE_TABLE(pci, intel_quark_mfd_ids);
  97. static int intel_quark_register_i2c_clk(struct intel_quark_mfd *quark_mfd)
  98. {
  99. struct pci_dev *pdev = quark_mfd->pdev;
  100. struct clk_lookup *i2c_clk_lookup;
  101. struct clk *i2c_clk;
  102. int ret;
  103. i2c_clk_lookup = devm_kcalloc(&pdev->dev, INTEL_QUARK_I2C_NCLK,
  104. sizeof(*i2c_clk_lookup), GFP_KERNEL);
  105. if (!i2c_clk_lookup)
  106. return -ENOMEM;
  107. i2c_clk_lookup[0].dev_id = INTEL_QUARK_I2C_CONTROLLER_CLK;
  108. i2c_clk = clk_register_fixed_rate(&pdev->dev,
  109. INTEL_QUARK_I2C_CONTROLLER_CLK, NULL,
  110. CLK_IS_ROOT, INTEL_QUARK_I2C_CLK_HZ);
  111. quark_mfd->i2c_clk_lookup = i2c_clk_lookup;
  112. quark_mfd->i2c_clk = i2c_clk;
  113. ret = clk_register_clkdevs(i2c_clk, i2c_clk_lookup,
  114. INTEL_QUARK_I2C_NCLK);
  115. if (ret)
  116. dev_err(&pdev->dev, "Fixed clk register failed: %d\n", ret);
  117. return ret;
  118. }
  119. static void intel_quark_unregister_i2c_clk(struct pci_dev *pdev)
  120. {
  121. struct intel_quark_mfd *quark_mfd = dev_get_drvdata(&pdev->dev);
  122. if (!quark_mfd->i2c_clk || !quark_mfd->i2c_clk_lookup)
  123. return;
  124. clkdev_drop(quark_mfd->i2c_clk_lookup);
  125. clk_unregister(quark_mfd->i2c_clk);
  126. }
  127. static int intel_quark_i2c_setup(struct pci_dev *pdev, struct mfd_cell *cell)
  128. {
  129. const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
  130. const struct i2c_mode_info *info;
  131. struct dw_i2c_platform_data *pdata;
  132. struct resource *res = (struct resource *)cell->resources;
  133. struct device *dev = &pdev->dev;
  134. res[INTEL_QUARK_IORES_MEM].start =
  135. pci_resource_start(pdev, MFD_I2C_BAR);
  136. res[INTEL_QUARK_IORES_MEM].end =
  137. pci_resource_end(pdev, MFD_I2C_BAR);
  138. res[INTEL_QUARK_IORES_IRQ].start = pdev->irq;
  139. res[INTEL_QUARK_IORES_IRQ].end = pdev->irq;
  140. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  141. if (!pdata)
  142. return -ENOMEM;
  143. /* Normal mode by default */
  144. pdata->i2c_scl_freq = 100000;
  145. if (board_name) {
  146. for (info = platform_i2c_mode_info; info->name; info++) {
  147. if (!strcmp(board_name, info->name)) {
  148. pdata->i2c_scl_freq = info->i2c_scl_freq;
  149. break;
  150. }
  151. }
  152. }
  153. cell->platform_data = pdata;
  154. cell->pdata_size = sizeof(*pdata);
  155. return 0;
  156. }
  157. static int intel_quark_gpio_setup(struct pci_dev *pdev, struct mfd_cell *cell)
  158. {
  159. struct dwapb_platform_data *pdata;
  160. struct resource *res = (struct resource *)cell->resources;
  161. struct device *dev = &pdev->dev;
  162. res[INTEL_QUARK_IORES_MEM].start =
  163. pci_resource_start(pdev, MFD_GPIO_BAR);
  164. res[INTEL_QUARK_IORES_MEM].end =
  165. pci_resource_end(pdev, MFD_GPIO_BAR);
  166. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  167. if (!pdata)
  168. return -ENOMEM;
  169. /* For intel quark x1000, it has only one port: portA */
  170. pdata->nports = INTEL_QUARK_GPIO_NPORTS;
  171. pdata->properties = devm_kcalloc(dev, pdata->nports,
  172. sizeof(*pdata->properties),
  173. GFP_KERNEL);
  174. if (!pdata->properties)
  175. return -ENOMEM;
  176. /* Set the properties for portA */
  177. pdata->properties->node = NULL;
  178. pdata->properties->name = "intel-quark-x1000-gpio-portA";
  179. pdata->properties->idx = 0;
  180. pdata->properties->ngpio = INTEL_QUARK_MFD_NGPIO;
  181. pdata->properties->gpio_base = INTEL_QUARK_MFD_GPIO_BASE;
  182. pdata->properties->irq = pdev->irq;
  183. pdata->properties->irq_shared = true;
  184. cell->platform_data = pdata;
  185. cell->pdata_size = sizeof(*pdata);
  186. return 0;
  187. }
  188. static int intel_quark_mfd_probe(struct pci_dev *pdev,
  189. const struct pci_device_id *id)
  190. {
  191. struct intel_quark_mfd *quark_mfd;
  192. int ret;
  193. ret = pcim_enable_device(pdev);
  194. if (ret)
  195. return ret;
  196. quark_mfd = devm_kzalloc(&pdev->dev, sizeof(*quark_mfd), GFP_KERNEL);
  197. if (!quark_mfd)
  198. return -ENOMEM;
  199. quark_mfd->pdev = pdev;
  200. ret = intel_quark_register_i2c_clk(quark_mfd);
  201. if (ret)
  202. return ret;
  203. dev_set_drvdata(&pdev->dev, quark_mfd);
  204. ret = intel_quark_i2c_setup(pdev, &intel_quark_mfd_cells[MFD_I2C_BAR]);
  205. if (ret)
  206. return ret;
  207. ret = intel_quark_gpio_setup(pdev,
  208. &intel_quark_mfd_cells[MFD_GPIO_BAR]);
  209. if (ret)
  210. return ret;
  211. return mfd_add_devices(&pdev->dev, 0, intel_quark_mfd_cells,
  212. ARRAY_SIZE(intel_quark_mfd_cells), NULL, 0,
  213. NULL);
  214. }
  215. static void intel_quark_mfd_remove(struct pci_dev *pdev)
  216. {
  217. intel_quark_unregister_i2c_clk(pdev);
  218. mfd_remove_devices(&pdev->dev);
  219. }
  220. static struct pci_driver intel_quark_mfd_driver = {
  221. .name = "intel_quark_mfd_i2c_gpio",
  222. .id_table = intel_quark_mfd_ids,
  223. .probe = intel_quark_mfd_probe,
  224. .remove = intel_quark_mfd_remove,
  225. };
  226. module_pci_driver(intel_quark_mfd_driver);
  227. MODULE_AUTHOR("Raymond Tan <raymond.tan@intel.com>");
  228. MODULE_DESCRIPTION("Intel Quark MFD PCI driver for I2C & GPIO");
  229. MODULE_LICENSE("GPL v2");