pinctrl-mcp23s08_spi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* MCP23S08 SPI GPIO driver */
  3. #include <linux/mod_devicetable.h>
  4. #include <linux/module.h>
  5. #include <linux/property.h>
  6. #include <linux/regmap.h>
  7. #include <linux/spi/spi.h>
  8. #include "pinctrl-mcp23s08.h"
  9. #define MCP_MAX_DEV_PER_CS 8
  10. /*
  11. * A given spi_device can represent up to eight mcp23sxx chips
  12. * sharing the same chipselect but using different addresses
  13. * (e.g. chips #0 and #3 might be populated, but not #1 or #2).
  14. * Driver data holds all the per-chip data.
  15. */
  16. struct mcp23s08_driver_data {
  17. unsigned ngpio;
  18. struct mcp23s08 *mcp[8];
  19. struct mcp23s08 chip[];
  20. };
  21. static int mcp23sxx_spi_write(void *context, const void *data, size_t count)
  22. {
  23. struct mcp23s08 *mcp = context;
  24. struct spi_device *spi = to_spi_device(mcp->dev);
  25. struct spi_message m;
  26. struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, },
  27. { .tx_buf = data, .len = count, }, };
  28. spi_message_init(&m);
  29. spi_message_add_tail(&t[0], &m);
  30. spi_message_add_tail(&t[1], &m);
  31. return spi_sync(spi, &m);
  32. }
  33. static int mcp23sxx_spi_gather_write(void *context,
  34. const void *reg, size_t reg_size,
  35. const void *val, size_t val_size)
  36. {
  37. struct mcp23s08 *mcp = context;
  38. struct spi_device *spi = to_spi_device(mcp->dev);
  39. struct spi_message m;
  40. struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, },
  41. { .tx_buf = reg, .len = reg_size, },
  42. { .tx_buf = val, .len = val_size, }, };
  43. spi_message_init(&m);
  44. spi_message_add_tail(&t[0], &m);
  45. spi_message_add_tail(&t[1], &m);
  46. spi_message_add_tail(&t[2], &m);
  47. return spi_sync(spi, &m);
  48. }
  49. static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size,
  50. void *val, size_t val_size)
  51. {
  52. struct mcp23s08 *mcp = context;
  53. struct spi_device *spi = to_spi_device(mcp->dev);
  54. u8 tx[2];
  55. if (reg_size != 1)
  56. return -EINVAL;
  57. tx[0] = mcp->addr | 0x01;
  58. tx[1] = *((u8 *) reg);
  59. return spi_write_then_read(spi, tx, sizeof(tx), val, val_size);
  60. }
  61. static const struct regmap_bus mcp23sxx_spi_regmap = {
  62. .write = mcp23sxx_spi_write,
  63. .gather_write = mcp23sxx_spi_gather_write,
  64. .read = mcp23sxx_spi_read,
  65. };
  66. static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
  67. unsigned int addr, unsigned int type)
  68. {
  69. const struct regmap_config *config;
  70. struct regmap_config *copy;
  71. const char *name;
  72. switch (type) {
  73. case MCP_TYPE_S08:
  74. mcp->reg_shift = 0;
  75. mcp->chip.ngpio = 8;
  76. mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s08.%d", addr);
  77. if (!mcp->chip.label)
  78. return -ENOMEM;
  79. config = &mcp23x08_regmap;
  80. name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr);
  81. if (!name)
  82. return -ENOMEM;
  83. break;
  84. case MCP_TYPE_S17:
  85. mcp->reg_shift = 1;
  86. mcp->chip.ngpio = 16;
  87. mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s17.%d", addr);
  88. if (!mcp->chip.label)
  89. return -ENOMEM;
  90. config = &mcp23x17_regmap;
  91. name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr);
  92. if (!name)
  93. return -ENOMEM;
  94. break;
  95. case MCP_TYPE_S18:
  96. mcp->reg_shift = 1;
  97. mcp->chip.ngpio = 16;
  98. mcp->chip.label = "mcp23s18";
  99. config = &mcp23x17_regmap;
  100. name = config->name;
  101. break;
  102. default:
  103. dev_err(dev, "invalid device type (%d)\n", type);
  104. return -EINVAL;
  105. }
  106. copy = devm_kmemdup(dev, config, sizeof(*config), GFP_KERNEL);
  107. if (!copy)
  108. return -ENOMEM;
  109. copy->name = name;
  110. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp, copy);
  111. if (IS_ERR(mcp->regmap))
  112. dev_err(dev, "regmap init failed for %s\n", mcp->chip.label);
  113. return PTR_ERR_OR_ZERO(mcp->regmap);
  114. }
  115. static int mcp23s08_probe(struct spi_device *spi)
  116. {
  117. struct device *dev = &spi->dev;
  118. struct mcp23s08_driver_data *data;
  119. unsigned long spi_present_mask;
  120. const void *match;
  121. unsigned int addr;
  122. unsigned int ngpio = 0;
  123. int chips;
  124. int type;
  125. int ret;
  126. u32 v;
  127. match = device_get_match_data(dev);
  128. if (match)
  129. type = (int)(uintptr_t)match;
  130. else
  131. type = spi_get_device_id(spi)->driver_data;
  132. ret = device_property_read_u32(dev, "microchip,spi-present-mask", &v);
  133. if (ret) {
  134. ret = device_property_read_u32(dev, "mcp,spi-present-mask", &v);
  135. if (ret) {
  136. dev_err(dev, "missing spi-present-mask");
  137. return ret;
  138. }
  139. }
  140. spi_present_mask = v;
  141. if (!spi_present_mask || spi_present_mask >= BIT(MCP_MAX_DEV_PER_CS)) {
  142. dev_err(dev, "invalid spi-present-mask");
  143. return -ENODEV;
  144. }
  145. chips = hweight_long(spi_present_mask);
  146. data = devm_kzalloc(dev, struct_size(data, chip, chips), GFP_KERNEL);
  147. if (!data)
  148. return -ENOMEM;
  149. spi_set_drvdata(spi, data);
  150. for_each_set_bit(addr, &spi_present_mask, MCP_MAX_DEV_PER_CS) {
  151. data->mcp[addr] = &data->chip[--chips];
  152. data->mcp[addr]->irq = spi->irq;
  153. ret = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, type);
  154. if (ret)
  155. return ret;
  156. data->mcp[addr]->pinctrl_desc.name = devm_kasprintf(dev, GFP_KERNEL,
  157. "mcp23xxx-pinctrl.%d",
  158. addr);
  159. if (!data->mcp[addr]->pinctrl_desc.name)
  160. return -ENOMEM;
  161. ret = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1), type, -1);
  162. if (ret < 0)
  163. return ret;
  164. ngpio += data->mcp[addr]->chip.ngpio;
  165. }
  166. data->ngpio = ngpio;
  167. return 0;
  168. }
  169. static const struct spi_device_id mcp23s08_ids[] = {
  170. { "mcp23s08", MCP_TYPE_S08 },
  171. { "mcp23s17", MCP_TYPE_S17 },
  172. { "mcp23s18", MCP_TYPE_S18 },
  173. { }
  174. };
  175. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  176. static const struct of_device_id mcp23s08_spi_of_match[] = {
  177. {
  178. .compatible = "microchip,mcp23s08",
  179. .data = (void *) MCP_TYPE_S08,
  180. },
  181. {
  182. .compatible = "microchip,mcp23s17",
  183. .data = (void *) MCP_TYPE_S17,
  184. },
  185. {
  186. .compatible = "microchip,mcp23s18",
  187. .data = (void *) MCP_TYPE_S18,
  188. },
  189. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  190. {
  191. .compatible = "mcp,mcp23s08",
  192. .data = (void *) MCP_TYPE_S08,
  193. },
  194. {
  195. .compatible = "mcp,mcp23s17",
  196. .data = (void *) MCP_TYPE_S17,
  197. },
  198. { }
  199. };
  200. MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
  201. static struct spi_driver mcp23s08_driver = {
  202. .probe = mcp23s08_probe,
  203. .id_table = mcp23s08_ids,
  204. .driver = {
  205. .name = "mcp23s08",
  206. .of_match_table = mcp23s08_spi_of_match,
  207. },
  208. };
  209. static int __init mcp23s08_spi_init(void)
  210. {
  211. return spi_register_driver(&mcp23s08_driver);
  212. }
  213. /*
  214. * Register after SPI postcore initcall and before
  215. * subsys initcalls that may rely on these GPIOs.
  216. */
  217. subsys_initcall(mcp23s08_spi_init);
  218. static void mcp23s08_spi_exit(void)
  219. {
  220. spi_unregister_driver(&mcp23s08_driver);
  221. }
  222. module_exit(mcp23s08_spi_exit);
  223. MODULE_LICENSE("GPL");