moxtet.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Turris Mox module configuration bus driver
  4. *
  5. * Copyright (C) 2019 Marek Behún <kabel@kernel.org>
  6. */
  7. #include <dt-bindings/bus/moxtet.h>
  8. #include <linux/bitops.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/moxtet.h>
  13. #include <linux/mutex.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/spi/spi.h>
  17. /*
  18. * @name: module name for sysfs
  19. * @hwirq_base: base index for IRQ for this module (-1 if no IRQs)
  20. * @nirqs: how many interrupts does the shift register provide
  21. * @desc: module description for kernel log
  22. */
  23. static const struct {
  24. const char *name;
  25. int hwirq_base;
  26. int nirqs;
  27. const char *desc;
  28. } mox_module_table[] = {
  29. /* do not change order of this array! */
  30. { NULL, 0, 0, NULL },
  31. { "sfp", -1, 0, "MOX D (SFP cage)" },
  32. { "pci", MOXTET_IRQ_PCI, 1, "MOX B (Mini-PCIe)" },
  33. { "topaz", MOXTET_IRQ_TOPAZ, 1, "MOX C (4 port switch)" },
  34. { "peridot", MOXTET_IRQ_PERIDOT(0), 1, "MOX E (8 port switch)" },
  35. { "usb3", MOXTET_IRQ_USB3, 2, "MOX F (USB 3.0)" },
  36. { "pci-bridge", -1, 0, "MOX G (Mini-PCIe bridge)" },
  37. };
  38. static inline bool mox_module_known(unsigned int id)
  39. {
  40. return id >= TURRIS_MOX_MODULE_FIRST && id <= TURRIS_MOX_MODULE_LAST;
  41. }
  42. static inline const char *mox_module_name(unsigned int id)
  43. {
  44. if (mox_module_known(id))
  45. return mox_module_table[id].name;
  46. else
  47. return "unknown";
  48. }
  49. #define DEF_MODULE_ATTR(name, fmt, ...) \
  50. static ssize_t \
  51. module_##name##_show(struct device *dev, struct device_attribute *a, \
  52. char *buf) \
  53. { \
  54. struct moxtet_device *mdev = to_moxtet_device(dev); \
  55. return sprintf(buf, (fmt), __VA_ARGS__); \
  56. } \
  57. static DEVICE_ATTR_RO(module_##name)
  58. DEF_MODULE_ATTR(id, "0x%x\n", mdev->id);
  59. DEF_MODULE_ATTR(name, "%s\n", mox_module_name(mdev->id));
  60. DEF_MODULE_ATTR(description, "%s\n",
  61. mox_module_known(mdev->id) ? mox_module_table[mdev->id].desc
  62. : "");
  63. static struct attribute *moxtet_dev_attrs[] = {
  64. &dev_attr_module_id.attr,
  65. &dev_attr_module_name.attr,
  66. &dev_attr_module_description.attr,
  67. NULL,
  68. };
  69. static const struct attribute_group moxtet_dev_group = {
  70. .attrs = moxtet_dev_attrs,
  71. };
  72. static const struct attribute_group *moxtet_dev_groups[] = {
  73. &moxtet_dev_group,
  74. NULL,
  75. };
  76. static int moxtet_match(struct device *dev, const struct device_driver *drv)
  77. {
  78. struct moxtet_device *mdev = to_moxtet_device(dev);
  79. const struct moxtet_driver *tdrv = to_moxtet_driver(drv);
  80. const enum turris_mox_module_id *t;
  81. if (of_driver_match_device(dev, drv))
  82. return 1;
  83. if (!tdrv->id_table)
  84. return 0;
  85. for (t = tdrv->id_table; *t; ++t)
  86. if (*t == mdev->id)
  87. return 1;
  88. return 0;
  89. }
  90. static const struct bus_type moxtet_bus_type = {
  91. .name = "moxtet",
  92. .dev_groups = moxtet_dev_groups,
  93. .match = moxtet_match,
  94. };
  95. int __moxtet_register_driver(struct module *owner,
  96. struct moxtet_driver *mdrv)
  97. {
  98. mdrv->driver.owner = owner;
  99. mdrv->driver.bus = &moxtet_bus_type;
  100. return driver_register(&mdrv->driver);
  101. }
  102. EXPORT_SYMBOL_GPL(__moxtet_register_driver);
  103. static int moxtet_dev_check(struct device *dev, void *data)
  104. {
  105. struct moxtet_device *mdev = to_moxtet_device(dev);
  106. struct moxtet_device *new_dev = data;
  107. if (mdev->moxtet == new_dev->moxtet && mdev->id == new_dev->id &&
  108. mdev->idx == new_dev->idx)
  109. return -EBUSY;
  110. return 0;
  111. }
  112. static void moxtet_dev_release(struct device *dev)
  113. {
  114. struct moxtet_device *mdev = to_moxtet_device(dev);
  115. put_device(mdev->moxtet->dev);
  116. kfree(mdev);
  117. }
  118. static struct moxtet_device *
  119. moxtet_alloc_device(struct moxtet *moxtet)
  120. {
  121. struct moxtet_device *dev;
  122. if (!get_device(moxtet->dev))
  123. return NULL;
  124. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  125. if (!dev) {
  126. put_device(moxtet->dev);
  127. return NULL;
  128. }
  129. dev->moxtet = moxtet;
  130. dev->dev.parent = moxtet->dev;
  131. dev->dev.bus = &moxtet_bus_type;
  132. dev->dev.release = moxtet_dev_release;
  133. device_initialize(&dev->dev);
  134. return dev;
  135. }
  136. static int moxtet_add_device(struct moxtet_device *dev)
  137. {
  138. static DEFINE_MUTEX(add_mutex);
  139. int ret;
  140. if (dev->idx >= TURRIS_MOX_MAX_MODULES || dev->id > 0xf)
  141. return -EINVAL;
  142. dev_set_name(&dev->dev, "moxtet-%s.%u", mox_module_name(dev->id),
  143. dev->idx);
  144. mutex_lock(&add_mutex);
  145. ret = bus_for_each_dev(&moxtet_bus_type, NULL, dev,
  146. moxtet_dev_check);
  147. if (ret)
  148. goto done;
  149. ret = device_add(&dev->dev);
  150. if (ret < 0)
  151. dev_err(dev->moxtet->dev, "can't add %s, status %d\n",
  152. dev_name(dev->moxtet->dev), ret);
  153. done:
  154. mutex_unlock(&add_mutex);
  155. return ret;
  156. }
  157. static int __unregister(struct device *dev, void *null)
  158. {
  159. if (dev->of_node) {
  160. of_node_clear_flag(dev->of_node, OF_POPULATED);
  161. of_node_put(dev->of_node);
  162. }
  163. device_unregister(dev);
  164. return 0;
  165. }
  166. static struct moxtet_device *
  167. of_register_moxtet_device(struct moxtet *moxtet, struct device_node *nc)
  168. {
  169. struct moxtet_device *dev;
  170. u32 val;
  171. int ret;
  172. dev = moxtet_alloc_device(moxtet);
  173. if (!dev) {
  174. dev_err(moxtet->dev,
  175. "Moxtet device alloc error for %pOF\n", nc);
  176. return ERR_PTR(-ENOMEM);
  177. }
  178. ret = of_property_read_u32(nc, "reg", &val);
  179. if (ret) {
  180. dev_err(moxtet->dev, "%pOF has no valid 'reg' property (%d)\n",
  181. nc, ret);
  182. goto err_put;
  183. }
  184. dev->idx = val;
  185. if (dev->idx >= TURRIS_MOX_MAX_MODULES) {
  186. dev_err(moxtet->dev, "%pOF Moxtet address 0x%x out of range\n",
  187. nc, dev->idx);
  188. ret = -EINVAL;
  189. goto err_put;
  190. }
  191. dev->id = moxtet->modules[dev->idx];
  192. if (!dev->id) {
  193. dev_err(moxtet->dev, "%pOF Moxtet address 0x%x is empty\n", nc,
  194. dev->idx);
  195. ret = -ENODEV;
  196. goto err_put;
  197. }
  198. of_node_get(nc);
  199. dev->dev.of_node = nc;
  200. ret = moxtet_add_device(dev);
  201. if (ret) {
  202. dev_err(moxtet->dev,
  203. "Moxtet device register error for %pOF\n", nc);
  204. of_node_put(nc);
  205. goto err_put;
  206. }
  207. return dev;
  208. err_put:
  209. put_device(&dev->dev);
  210. return ERR_PTR(ret);
  211. }
  212. static void of_register_moxtet_devices(struct moxtet *moxtet)
  213. {
  214. struct moxtet_device *dev;
  215. struct device_node *nc;
  216. if (!moxtet->dev->of_node)
  217. return;
  218. for_each_available_child_of_node(moxtet->dev->of_node, nc) {
  219. if (of_node_test_and_set_flag(nc, OF_POPULATED))
  220. continue;
  221. dev = of_register_moxtet_device(moxtet, nc);
  222. if (IS_ERR(dev)) {
  223. dev_warn(moxtet->dev,
  224. "Failed to create Moxtet device for %pOF\n",
  225. nc);
  226. of_node_clear_flag(nc, OF_POPULATED);
  227. }
  228. }
  229. }
  230. static void
  231. moxtet_register_devices_from_topology(struct moxtet *moxtet)
  232. {
  233. struct moxtet_device *dev;
  234. int i, ret;
  235. for (i = 0; i < moxtet->count; ++i) {
  236. dev = moxtet_alloc_device(moxtet);
  237. if (!dev) {
  238. dev_err(moxtet->dev, "Moxtet device %u alloc error\n",
  239. i);
  240. continue;
  241. }
  242. dev->idx = i;
  243. dev->id = moxtet->modules[i];
  244. ret = moxtet_add_device(dev);
  245. if (ret && ret != -EBUSY) {
  246. put_device(&dev->dev);
  247. dev_err(moxtet->dev,
  248. "Moxtet device %u register error: %i\n", i,
  249. ret);
  250. }
  251. }
  252. }
  253. /*
  254. * @nsame: how many modules with same id are already in moxtet->modules
  255. */
  256. static int moxtet_set_irq(struct moxtet *moxtet, int idx, int id, int nsame)
  257. {
  258. int i, first;
  259. struct moxtet_irqpos *pos;
  260. first = mox_module_table[id].hwirq_base +
  261. nsame * mox_module_table[id].nirqs;
  262. if (first + mox_module_table[id].nirqs > MOXTET_NIRQS)
  263. return -EINVAL;
  264. for (i = 0; i < mox_module_table[id].nirqs; ++i) {
  265. pos = &moxtet->irq.position[first + i];
  266. pos->idx = idx;
  267. pos->bit = i;
  268. moxtet->irq.exists |= BIT(first + i);
  269. }
  270. return 0;
  271. }
  272. static int moxtet_find_topology(struct moxtet *moxtet)
  273. {
  274. u8 buf[TURRIS_MOX_MAX_MODULES];
  275. int cnts[TURRIS_MOX_MODULE_LAST];
  276. int i, ret;
  277. memset(cnts, 0, sizeof(cnts));
  278. ret = spi_read(to_spi_device(moxtet->dev), buf, TURRIS_MOX_MAX_MODULES);
  279. if (ret < 0)
  280. return ret;
  281. if (buf[0] == TURRIS_MOX_CPU_ID_EMMC) {
  282. dev_info(moxtet->dev, "Found MOX A (eMMC CPU) module\n");
  283. } else if (buf[0] == TURRIS_MOX_CPU_ID_SD) {
  284. dev_info(moxtet->dev, "Found MOX A (CPU) module\n");
  285. } else {
  286. dev_err(moxtet->dev, "Invalid Turris MOX A CPU module 0x%02x\n",
  287. buf[0]);
  288. return -ENODEV;
  289. }
  290. moxtet->count = 0;
  291. for (i = 1; i < TURRIS_MOX_MAX_MODULES; ++i) {
  292. int id;
  293. if (buf[i] == 0xff)
  294. break;
  295. id = buf[i] & 0xf;
  296. moxtet->modules[i-1] = id;
  297. ++moxtet->count;
  298. if (mox_module_known(id)) {
  299. dev_info(moxtet->dev, "Found %s module\n",
  300. mox_module_table[id].desc);
  301. if (moxtet_set_irq(moxtet, i-1, id, cnts[id]++) < 0)
  302. dev_err(moxtet->dev,
  303. " Cannot set IRQ for module %s\n",
  304. mox_module_table[id].desc);
  305. } else {
  306. dev_warn(moxtet->dev,
  307. "Unknown Moxtet module found (ID 0x%02x)\n",
  308. id);
  309. }
  310. }
  311. return 0;
  312. }
  313. static int moxtet_spi_read(struct moxtet *moxtet, u8 *buf)
  314. {
  315. struct spi_transfer xfer = {
  316. .rx_buf = buf,
  317. .tx_buf = moxtet->tx,
  318. .len = moxtet->count + 1
  319. };
  320. int ret;
  321. mutex_lock(&moxtet->lock);
  322. ret = spi_sync_transfer(to_spi_device(moxtet->dev), &xfer, 1);
  323. mutex_unlock(&moxtet->lock);
  324. return ret;
  325. }
  326. int moxtet_device_read(struct device *dev)
  327. {
  328. struct moxtet_device *mdev = to_moxtet_device(dev);
  329. struct moxtet *moxtet = mdev->moxtet;
  330. u8 buf[TURRIS_MOX_MAX_MODULES];
  331. int ret;
  332. if (mdev->idx >= moxtet->count)
  333. return -EINVAL;
  334. ret = moxtet_spi_read(moxtet, buf);
  335. if (ret < 0)
  336. return ret;
  337. return buf[mdev->idx + 1] >> 4;
  338. }
  339. EXPORT_SYMBOL_GPL(moxtet_device_read);
  340. int moxtet_device_write(struct device *dev, u8 val)
  341. {
  342. struct moxtet_device *mdev = to_moxtet_device(dev);
  343. struct moxtet *moxtet = mdev->moxtet;
  344. int ret;
  345. if (mdev->idx >= moxtet->count)
  346. return -EINVAL;
  347. mutex_lock(&moxtet->lock);
  348. moxtet->tx[moxtet->count - mdev->idx] = val;
  349. ret = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
  350. moxtet->count + 1);
  351. mutex_unlock(&moxtet->lock);
  352. return ret;
  353. }
  354. EXPORT_SYMBOL_GPL(moxtet_device_write);
  355. int moxtet_device_written(struct device *dev)
  356. {
  357. struct moxtet_device *mdev = to_moxtet_device(dev);
  358. struct moxtet *moxtet = mdev->moxtet;
  359. if (mdev->idx >= moxtet->count)
  360. return -EINVAL;
  361. return moxtet->tx[moxtet->count - mdev->idx];
  362. }
  363. EXPORT_SYMBOL_GPL(moxtet_device_written);
  364. #ifdef CONFIG_DEBUG_FS
  365. static int moxtet_debug_open(struct inode *inode, struct file *file)
  366. {
  367. file->private_data = inode->i_private;
  368. return nonseekable_open(inode, file);
  369. }
  370. static ssize_t input_read(struct file *file, char __user *buf, size_t len,
  371. loff_t *ppos)
  372. {
  373. struct moxtet *moxtet = file->private_data;
  374. u8 bin[TURRIS_MOX_MAX_MODULES];
  375. u8 hex[sizeof(bin) * 2 + 1];
  376. int ret, n;
  377. ret = moxtet_spi_read(moxtet, bin);
  378. if (ret < 0)
  379. return ret;
  380. n = moxtet->count + 1;
  381. bin2hex(hex, bin, n);
  382. hex[2*n] = '\n';
  383. return simple_read_from_buffer(buf, len, ppos, hex, 2*n + 1);
  384. }
  385. static const struct file_operations input_fops = {
  386. .owner = THIS_MODULE,
  387. .open = moxtet_debug_open,
  388. .read = input_read,
  389. };
  390. static ssize_t output_read(struct file *file, char __user *buf, size_t len,
  391. loff_t *ppos)
  392. {
  393. struct moxtet *moxtet = file->private_data;
  394. u8 hex[TURRIS_MOX_MAX_MODULES * 2 + 1];
  395. u8 *p = hex;
  396. int i;
  397. mutex_lock(&moxtet->lock);
  398. for (i = 0; i < moxtet->count; ++i)
  399. p = hex_byte_pack(p, moxtet->tx[moxtet->count - i]);
  400. mutex_unlock(&moxtet->lock);
  401. *p++ = '\n';
  402. return simple_read_from_buffer(buf, len, ppos, hex, p - hex);
  403. }
  404. static ssize_t output_write(struct file *file, const char __user *buf,
  405. size_t len, loff_t *ppos)
  406. {
  407. struct moxtet *moxtet = file->private_data;
  408. u8 bin[TURRIS_MOX_MAX_MODULES];
  409. u8 hex[sizeof(bin) * 2 + 1];
  410. ssize_t res;
  411. loff_t dummy = 0;
  412. int err, i;
  413. if (len > 2 * moxtet->count + 1 || len < 2 * moxtet->count)
  414. return -EINVAL;
  415. res = simple_write_to_buffer(hex, sizeof(hex), &dummy, buf, len);
  416. if (res < 0)
  417. return res;
  418. if (len % 2 == 1 && hex[len - 1] != '\n')
  419. return -EINVAL;
  420. err = hex2bin(bin, hex, moxtet->count);
  421. if (err < 0)
  422. return -EINVAL;
  423. mutex_lock(&moxtet->lock);
  424. for (i = 0; i < moxtet->count; ++i)
  425. moxtet->tx[moxtet->count - i] = bin[i];
  426. err = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
  427. moxtet->count + 1);
  428. mutex_unlock(&moxtet->lock);
  429. return err < 0 ? err : len;
  430. }
  431. static const struct file_operations output_fops = {
  432. .owner = THIS_MODULE,
  433. .open = moxtet_debug_open,
  434. .read = output_read,
  435. .write = output_write,
  436. };
  437. static int moxtet_register_debugfs(struct moxtet *moxtet)
  438. {
  439. struct dentry *root, *entry;
  440. root = debugfs_create_dir("moxtet", NULL);
  441. if (IS_ERR(root))
  442. return PTR_ERR(root);
  443. entry = debugfs_create_file_unsafe("input", 0444, root, moxtet,
  444. &input_fops);
  445. if (IS_ERR(entry))
  446. goto err_remove;
  447. entry = debugfs_create_file_unsafe("output", 0644, root, moxtet,
  448. &output_fops);
  449. if (IS_ERR(entry))
  450. goto err_remove;
  451. moxtet->debugfs_root = root;
  452. return 0;
  453. err_remove:
  454. debugfs_remove_recursive(root);
  455. return PTR_ERR(entry);
  456. }
  457. static void moxtet_unregister_debugfs(struct moxtet *moxtet)
  458. {
  459. debugfs_remove_recursive(moxtet->debugfs_root);
  460. }
  461. #else
  462. static inline int moxtet_register_debugfs(struct moxtet *moxtet)
  463. {
  464. return 0;
  465. }
  466. static inline void moxtet_unregister_debugfs(struct moxtet *moxtet)
  467. {
  468. }
  469. #endif
  470. static int moxtet_irq_domain_map(struct irq_domain *d, unsigned int irq,
  471. irq_hw_number_t hw)
  472. {
  473. struct moxtet *moxtet = d->host_data;
  474. if (hw >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(hw))) {
  475. dev_err(moxtet->dev, "Invalid hw irq number\n");
  476. return -EINVAL;
  477. }
  478. irq_set_chip_data(irq, d->host_data);
  479. irq_set_chip_and_handler(irq, &moxtet->irq.chip, handle_level_irq);
  480. return 0;
  481. }
  482. static int moxtet_irq_domain_xlate(struct irq_domain *d,
  483. struct device_node *ctrlr,
  484. const u32 *intspec, unsigned int intsize,
  485. unsigned long *out_hwirq,
  486. unsigned int *out_type)
  487. {
  488. struct moxtet *moxtet = d->host_data;
  489. int irq;
  490. if (WARN_ON(intsize < 1))
  491. return -EINVAL;
  492. irq = intspec[0];
  493. if (irq >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(irq)))
  494. return -EINVAL;
  495. *out_hwirq = irq;
  496. *out_type = IRQ_TYPE_NONE;
  497. return 0;
  498. }
  499. static const struct irq_domain_ops moxtet_irq_domain = {
  500. .map = moxtet_irq_domain_map,
  501. .xlate = moxtet_irq_domain_xlate,
  502. };
  503. static void moxtet_irq_mask(struct irq_data *d)
  504. {
  505. struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
  506. moxtet->irq.masked |= BIT(d->hwirq);
  507. }
  508. static void moxtet_irq_unmask(struct irq_data *d)
  509. {
  510. struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
  511. moxtet->irq.masked &= ~BIT(d->hwirq);
  512. }
  513. static void moxtet_irq_print_chip(struct irq_data *d, struct seq_file *p)
  514. {
  515. struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
  516. struct moxtet_irqpos *pos = &moxtet->irq.position[d->hwirq];
  517. int id;
  518. id = moxtet->modules[pos->idx];
  519. seq_printf(p, " moxtet-%s.%i#%i", mox_module_name(id), pos->idx,
  520. pos->bit);
  521. }
  522. static const struct irq_chip moxtet_irq_chip = {
  523. .name = "moxtet",
  524. .irq_mask = moxtet_irq_mask,
  525. .irq_unmask = moxtet_irq_unmask,
  526. .irq_print_chip = moxtet_irq_print_chip,
  527. };
  528. static int moxtet_irq_read(struct moxtet *moxtet, unsigned long *map)
  529. {
  530. struct moxtet_irqpos *pos = moxtet->irq.position;
  531. u8 buf[TURRIS_MOX_MAX_MODULES];
  532. int i, ret;
  533. ret = moxtet_spi_read(moxtet, buf);
  534. if (ret < 0)
  535. return ret;
  536. *map = 0;
  537. for_each_set_bit(i, &moxtet->irq.exists, MOXTET_NIRQS) {
  538. if (!(buf[pos[i].idx + 1] & BIT(4 + pos[i].bit)))
  539. set_bit(i, map);
  540. }
  541. return 0;
  542. }
  543. static irqreturn_t moxtet_irq_thread_fn(int irq, void *data)
  544. {
  545. struct moxtet *moxtet = data;
  546. unsigned long set;
  547. int nhandled = 0, i, sub_irq, ret;
  548. ret = moxtet_irq_read(moxtet, &set);
  549. if (ret < 0)
  550. goto out;
  551. set &= ~moxtet->irq.masked;
  552. do {
  553. for_each_set_bit(i, &set, MOXTET_NIRQS) {
  554. sub_irq = irq_find_mapping(moxtet->irq.domain, i);
  555. handle_nested_irq(sub_irq);
  556. dev_dbg(moxtet->dev, "%i irq\n", i);
  557. ++nhandled;
  558. }
  559. ret = moxtet_irq_read(moxtet, &set);
  560. if (ret < 0)
  561. goto out;
  562. set &= ~moxtet->irq.masked;
  563. } while (set);
  564. out:
  565. return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
  566. }
  567. static void moxtet_irq_free(struct moxtet *moxtet)
  568. {
  569. int i, irq;
  570. for (i = 0; i < MOXTET_NIRQS; ++i) {
  571. if (moxtet->irq.exists & BIT(i)) {
  572. irq = irq_find_mapping(moxtet->irq.domain, i);
  573. irq_dispose_mapping(irq);
  574. }
  575. }
  576. irq_domain_remove(moxtet->irq.domain);
  577. }
  578. static int moxtet_irq_setup(struct moxtet *moxtet)
  579. {
  580. int i, ret;
  581. moxtet->irq.domain = irq_domain_add_simple(moxtet->dev->of_node,
  582. MOXTET_NIRQS, 0,
  583. &moxtet_irq_domain, moxtet);
  584. if (moxtet->irq.domain == NULL) {
  585. dev_err(moxtet->dev, "Could not add IRQ domain\n");
  586. return -ENOMEM;
  587. }
  588. for (i = 0; i < MOXTET_NIRQS; ++i)
  589. if (moxtet->irq.exists & BIT(i))
  590. irq_create_mapping(moxtet->irq.domain, i);
  591. moxtet->irq.chip = moxtet_irq_chip;
  592. moxtet->irq.masked = ~0;
  593. ret = request_threaded_irq(moxtet->dev_irq, NULL, moxtet_irq_thread_fn,
  594. IRQF_SHARED | IRQF_ONESHOT, "moxtet", moxtet);
  595. if (ret < 0)
  596. goto err_free;
  597. return 0;
  598. err_free:
  599. moxtet_irq_free(moxtet);
  600. return ret;
  601. }
  602. static int moxtet_probe(struct spi_device *spi)
  603. {
  604. struct moxtet *moxtet;
  605. int ret;
  606. ret = spi_setup(spi);
  607. if (ret < 0)
  608. return ret;
  609. moxtet = devm_kzalloc(&spi->dev, sizeof(struct moxtet),
  610. GFP_KERNEL);
  611. if (!moxtet)
  612. return -ENOMEM;
  613. moxtet->dev = &spi->dev;
  614. spi_set_drvdata(spi, moxtet);
  615. mutex_init(&moxtet->lock);
  616. moxtet->dev_irq = of_irq_get(moxtet->dev->of_node, 0);
  617. if (moxtet->dev_irq == -EPROBE_DEFER)
  618. return -EPROBE_DEFER;
  619. if (moxtet->dev_irq <= 0) {
  620. dev_err(moxtet->dev, "No IRQ resource found\n");
  621. return -ENXIO;
  622. }
  623. ret = moxtet_find_topology(moxtet);
  624. if (ret < 0)
  625. return ret;
  626. if (moxtet->irq.exists) {
  627. ret = moxtet_irq_setup(moxtet);
  628. if (ret < 0)
  629. return ret;
  630. }
  631. of_register_moxtet_devices(moxtet);
  632. moxtet_register_devices_from_topology(moxtet);
  633. ret = moxtet_register_debugfs(moxtet);
  634. if (ret < 0)
  635. dev_warn(moxtet->dev, "Failed creating debugfs entries: %i\n",
  636. ret);
  637. return 0;
  638. }
  639. static void moxtet_remove(struct spi_device *spi)
  640. {
  641. struct moxtet *moxtet = spi_get_drvdata(spi);
  642. free_irq(moxtet->dev_irq, moxtet);
  643. moxtet_irq_free(moxtet);
  644. moxtet_unregister_debugfs(moxtet);
  645. device_for_each_child(moxtet->dev, NULL, __unregister);
  646. mutex_destroy(&moxtet->lock);
  647. }
  648. static const struct spi_device_id moxtet_spi_ids[] = {
  649. { "moxtet" },
  650. { },
  651. };
  652. MODULE_DEVICE_TABLE(spi, moxtet_spi_ids);
  653. static const struct of_device_id moxtet_dt_ids[] = {
  654. { .compatible = "cznic,moxtet" },
  655. {},
  656. };
  657. MODULE_DEVICE_TABLE(of, moxtet_dt_ids);
  658. static struct spi_driver moxtet_spi_driver = {
  659. .driver = {
  660. .name = "moxtet",
  661. .of_match_table = moxtet_dt_ids,
  662. },
  663. .id_table = moxtet_spi_ids,
  664. .probe = moxtet_probe,
  665. .remove = moxtet_remove,
  666. };
  667. static int __init moxtet_init(void)
  668. {
  669. int ret;
  670. ret = bus_register(&moxtet_bus_type);
  671. if (ret < 0) {
  672. pr_err("moxtet bus registration failed: %d\n", ret);
  673. goto error;
  674. }
  675. ret = spi_register_driver(&moxtet_spi_driver);
  676. if (ret < 0) {
  677. pr_err("moxtet spi driver registration failed: %d\n", ret);
  678. goto error_bus;
  679. }
  680. return 0;
  681. error_bus:
  682. bus_unregister(&moxtet_bus_type);
  683. error:
  684. return ret;
  685. }
  686. postcore_initcall_sync(moxtet_init);
  687. static void __exit moxtet_exit(void)
  688. {
  689. spi_unregister_driver(&moxtet_spi_driver);
  690. bus_unregister(&moxtet_bus_type);
  691. }
  692. module_exit(moxtet_exit);
  693. MODULE_AUTHOR("Marek Behun <kabel@kernel.org>");
  694. MODULE_DESCRIPTION("CZ.NIC's Turris Mox module configuration bus");
  695. MODULE_LICENSE("GPL v2");