pinctrl-mcp23s08_spi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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,
  68. const struct mcp23s08_info *info)
  69. {
  70. struct regmap_config *copy;
  71. const char *name;
  72. switch (info->type) {
  73. case MCP_TYPE_S08:
  74. mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s08.%d", addr);
  75. if (!mcp->chip.label)
  76. return -ENOMEM;
  77. name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr);
  78. if (!name)
  79. return -ENOMEM;
  80. break;
  81. case MCP_TYPE_S17:
  82. mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s17.%d", addr);
  83. if (!mcp->chip.label)
  84. return -ENOMEM;
  85. name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr);
  86. if (!name)
  87. return -ENOMEM;
  88. break;
  89. case MCP_TYPE_S18:
  90. mcp->chip.label = info->label;
  91. name = info->regmap->name;
  92. break;
  93. default:
  94. dev_err(dev, "invalid device type (%d)\n", info->type);
  95. return -EINVAL;
  96. }
  97. mcp->reg_shift = info->reg_shift;
  98. mcp->chip.ngpio = info->ngpio;
  99. copy = devm_kmemdup(dev, info->regmap, sizeof(*info->regmap), GFP_KERNEL);
  100. if (!copy)
  101. return -ENOMEM;
  102. copy->name = name;
  103. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp, copy);
  104. if (IS_ERR(mcp->regmap))
  105. dev_err(dev, "regmap init failed for %s\n", mcp->chip.label);
  106. return PTR_ERR_OR_ZERO(mcp->regmap);
  107. }
  108. static int mcp23s08_probe(struct spi_device *spi)
  109. {
  110. struct mcp23s08_driver_data *data;
  111. const struct mcp23s08_info *info;
  112. struct device *dev = &spi->dev;
  113. unsigned long spi_present_mask;
  114. unsigned int ngpio = 0;
  115. unsigned int addr;
  116. int chips;
  117. int ret;
  118. u32 v;
  119. info = spi_get_device_match_data(spi);
  120. ret = device_property_read_u32(dev, "microchip,spi-present-mask", &v);
  121. if (ret) {
  122. ret = device_property_read_u32(dev, "mcp,spi-present-mask", &v);
  123. if (ret) {
  124. dev_err(dev, "missing spi-present-mask");
  125. return ret;
  126. }
  127. }
  128. spi_present_mask = v;
  129. if (!spi_present_mask || spi_present_mask >= BIT(MCP_MAX_DEV_PER_CS)) {
  130. dev_err(dev, "invalid spi-present-mask");
  131. return -ENODEV;
  132. }
  133. chips = hweight_long(spi_present_mask);
  134. data = devm_kzalloc(dev, struct_size(data, chip, chips), GFP_KERNEL);
  135. if (!data)
  136. return -ENOMEM;
  137. spi_set_drvdata(spi, data);
  138. for_each_set_bit(addr, &spi_present_mask, MCP_MAX_DEV_PER_CS) {
  139. data->mcp[addr] = &data->chip[--chips];
  140. data->mcp[addr]->irq = spi->irq;
  141. ret = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, info);
  142. if (ret)
  143. return ret;
  144. data->mcp[addr]->pinctrl_desc.name = devm_kasprintf(dev, GFP_KERNEL,
  145. "mcp23xxx-pinctrl.%d",
  146. addr);
  147. if (!data->mcp[addr]->pinctrl_desc.name)
  148. return -ENOMEM;
  149. ret = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1),
  150. info->type, -1);
  151. if (ret < 0)
  152. return ret;
  153. ngpio += data->mcp[addr]->chip.ngpio;
  154. }
  155. data->ngpio = ngpio;
  156. return 0;
  157. }
  158. static const struct mcp23s08_info mcp23s08_spi = {
  159. .regmap = &mcp23x08_regmap,
  160. .type = MCP_TYPE_S08,
  161. .ngpio = 8,
  162. .reg_shift = 0,
  163. };
  164. static const struct mcp23s08_info mcp23s17_spi = {
  165. .regmap = &mcp23x17_regmap,
  166. .type = MCP_TYPE_S17,
  167. .ngpio = 16,
  168. .reg_shift = 1,
  169. };
  170. static const struct mcp23s08_info mcp23s18_spi = {
  171. .regmap = &mcp23x17_regmap,
  172. .label = "mcp23s18",
  173. .type = MCP_TYPE_S18,
  174. .ngpio = 16,
  175. .reg_shift = 1,
  176. };
  177. static const struct spi_device_id mcp23s08_ids[] = {
  178. { "mcp23s08", (kernel_ulong_t)&mcp23s08_spi },
  179. { "mcp23s17", (kernel_ulong_t)&mcp23s17_spi },
  180. { "mcp23s18", (kernel_ulong_t)&mcp23s18_spi },
  181. { }
  182. };
  183. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  184. static const struct of_device_id mcp23s08_spi_of_match[] = {
  185. { .compatible = "microchip,mcp23s08", .data = &mcp23s08_spi },
  186. { .compatible = "microchip,mcp23s17", .data = &mcp23s17_spi },
  187. { .compatible = "microchip,mcp23s18", .data = &mcp23s18_spi },
  188. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  189. { .compatible = "mcp,mcp23s08", .data = &mcp23s08_spi },
  190. { .compatible = "mcp,mcp23s17", .data = &mcp23s17_spi },
  191. { }
  192. };
  193. MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
  194. static struct spi_driver mcp23s08_driver = {
  195. .probe = mcp23s08_probe,
  196. .id_table = mcp23s08_ids,
  197. .driver = {
  198. .name = "mcp23s08",
  199. .of_match_table = mcp23s08_spi_of_match,
  200. },
  201. };
  202. static int __init mcp23s08_spi_init(void)
  203. {
  204. return spi_register_driver(&mcp23s08_driver);
  205. }
  206. /*
  207. * Register after SPI postcore initcall and before
  208. * subsys initcalls that may rely on these GPIOs.
  209. */
  210. subsys_initcall(mcp23s08_spi_init);
  211. static void mcp23s08_spi_exit(void)
  212. {
  213. spi_unregister_driver(&mcp23s08_driver);
  214. }
  215. module_exit(mcp23s08_spi_exit);
  216. MODULE_DESCRIPTION("MCP23S08 SPI GPIO driver");
  217. MODULE_LICENSE("GPL");