spidev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Simple synchronous userspace interface to SPI devices
  3. *
  4. * Copyright (C) 2006 SWAPP
  5. * Andrea Paterniani <a.paterniani@swapp-eng.it>
  6. * Copyright (C) 2007 David Brownell (simplification, cleanup)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/fs.h>
  26. #include <linux/device.h>
  27. #include <linux/err.h>
  28. #include <linux/list.h>
  29. #include <linux/errno.h>
  30. #include <linux/mutex.h>
  31. #include <linux/slab.h>
  32. #include <linux/compat.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/spi/spidev.h>
  37. #include <linux/uaccess.h>
  38. /*
  39. * This supports access to SPI devices using normal userspace I/O calls.
  40. * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
  41. * and often mask message boundaries, full SPI support requires full duplex
  42. * transfers. There are several kinds of internal message boundaries to
  43. * handle chipselect management and other protocol options.
  44. *
  45. * SPI has a character major number assigned. We allocate minor numbers
  46. * dynamically using a bitmask. You must use hotplug tools, such as udev
  47. * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
  48. * nodes, since there is no fixed association of minor numbers with any
  49. * particular SPI bus or device.
  50. */
  51. #define SPIDEV_MAJOR 153 /* assigned */
  52. #define N_SPI_MINORS 32 /* ... up to 256 */
  53. static DECLARE_BITMAP(minors, N_SPI_MINORS);
  54. /* Bit masks for spi_device.mode management. Note that incorrect
  55. * settings for some settings can cause *lots* of trouble for other
  56. * devices on a shared bus:
  57. *
  58. * - CS_HIGH ... this device will be active when it shouldn't be
  59. * - 3WIRE ... when active, it won't behave as it should
  60. * - NO_CS ... there will be no explicit message boundaries; this
  61. * is completely incompatible with the shared bus model
  62. * - READY ... transfers may proceed when they shouldn't.
  63. *
  64. * REVISIT should changing those flags be privileged?
  65. */
  66. #define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
  67. | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
  68. | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
  69. | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)
  70. struct spidev_data {
  71. dev_t devt;
  72. spinlock_t spi_lock;
  73. struct spi_device *spi;
  74. struct list_head device_entry;
  75. /* buffer is NULL unless this device is open (users > 0) */
  76. struct mutex buf_lock;
  77. unsigned users;
  78. u8 *buffer;
  79. };
  80. static LIST_HEAD(device_list);
  81. static DEFINE_MUTEX(device_list_lock);
  82. static unsigned bufsiz = 4096;
  83. module_param(bufsiz, uint, S_IRUGO);
  84. MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
  85. /*-------------------------------------------------------------------------*/
  86. /*
  87. * We can't use the standard synchronous wrappers for file I/O; we
  88. * need to protect against async removal of the underlying spi_device.
  89. */
  90. static void spidev_complete(void *arg)
  91. {
  92. complete(arg);
  93. }
  94. static ssize_t
  95. spidev_sync(struct spidev_data *spidev, struct spi_message *message)
  96. {
  97. DECLARE_COMPLETION_ONSTACK(done);
  98. int status;
  99. message->complete = spidev_complete;
  100. message->context = &done;
  101. spin_lock_irq(&spidev->spi_lock);
  102. if (spidev->spi == NULL)
  103. status = -ESHUTDOWN;
  104. else
  105. status = spi_async(spidev->spi, message);
  106. spin_unlock_irq(&spidev->spi_lock);
  107. if (status == 0) {
  108. wait_for_completion(&done);
  109. status = message->status;
  110. if (status == 0)
  111. status = message->actual_length;
  112. }
  113. return status;
  114. }
  115. static inline ssize_t
  116. spidev_sync_write(struct spidev_data *spidev, size_t len)
  117. {
  118. struct spi_transfer t = {
  119. .tx_buf = spidev->buffer,
  120. .len = len,
  121. };
  122. struct spi_message m;
  123. spi_message_init(&m);
  124. spi_message_add_tail(&t, &m);
  125. return spidev_sync(spidev, &m);
  126. }
  127. static inline ssize_t
  128. spidev_sync_read(struct spidev_data *spidev, size_t len)
  129. {
  130. struct spi_transfer t = {
  131. .rx_buf = spidev->buffer,
  132. .len = len,
  133. };
  134. struct spi_message m;
  135. spi_message_init(&m);
  136. spi_message_add_tail(&t, &m);
  137. return spidev_sync(spidev, &m);
  138. }
  139. /*-------------------------------------------------------------------------*/
  140. /* Read-only message with current device setup */
  141. static ssize_t
  142. spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  143. {
  144. struct spidev_data *spidev;
  145. ssize_t status = 0;
  146. /* chipselect only toggles at start or end of operation */
  147. if (count > bufsiz)
  148. return -EMSGSIZE;
  149. spidev = filp->private_data;
  150. mutex_lock(&spidev->buf_lock);
  151. status = spidev_sync_read(spidev, count);
  152. if (status > 0) {
  153. unsigned long missing;
  154. missing = copy_to_user(buf, spidev->buffer, status);
  155. if (missing == status)
  156. status = -EFAULT;
  157. else
  158. status = status - missing;
  159. }
  160. mutex_unlock(&spidev->buf_lock);
  161. return status;
  162. }
  163. /* Write-only message with current device setup */
  164. static ssize_t
  165. spidev_write(struct file *filp, const char __user *buf,
  166. size_t count, loff_t *f_pos)
  167. {
  168. struct spidev_data *spidev;
  169. ssize_t status = 0;
  170. unsigned long missing;
  171. /* chipselect only toggles at start or end of operation */
  172. if (count > bufsiz)
  173. return -EMSGSIZE;
  174. spidev = filp->private_data;
  175. mutex_lock(&spidev->buf_lock);
  176. missing = copy_from_user(spidev->buffer, buf, count);
  177. if (missing == 0)
  178. status = spidev_sync_write(spidev, count);
  179. else
  180. status = -EFAULT;
  181. mutex_unlock(&spidev->buf_lock);
  182. return status;
  183. }
  184. static int spidev_message(struct spidev_data *spidev,
  185. struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
  186. {
  187. struct spi_message msg;
  188. struct spi_transfer *k_xfers;
  189. struct spi_transfer *k_tmp;
  190. struct spi_ioc_transfer *u_tmp;
  191. unsigned n, total;
  192. u8 *buf;
  193. int status = -EFAULT;
  194. spi_message_init(&msg);
  195. k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
  196. if (k_xfers == NULL)
  197. return -ENOMEM;
  198. /* Construct spi_message, copying any tx data to bounce buffer.
  199. * We walk the array of user-provided transfers, using each one
  200. * to initialize a kernel version of the same transfer.
  201. */
  202. buf = spidev->buffer;
  203. total = 0;
  204. for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
  205. n;
  206. n--, k_tmp++, u_tmp++) {
  207. k_tmp->len = u_tmp->len;
  208. total += k_tmp->len;
  209. if (total > bufsiz) {
  210. status = -EMSGSIZE;
  211. goto done;
  212. }
  213. if (u_tmp->rx_buf) {
  214. k_tmp->rx_buf = buf;
  215. if (!access_ok(VERIFY_WRITE, (u8 __user *)
  216. (uintptr_t) u_tmp->rx_buf,
  217. u_tmp->len))
  218. goto done;
  219. }
  220. if (u_tmp->tx_buf) {
  221. k_tmp->tx_buf = buf;
  222. if (copy_from_user(buf, (const u8 __user *)
  223. (uintptr_t) u_tmp->tx_buf,
  224. u_tmp->len))
  225. goto done;
  226. }
  227. buf += k_tmp->len;
  228. k_tmp->cs_change = !!u_tmp->cs_change;
  229. k_tmp->tx_nbits = u_tmp->tx_nbits;
  230. k_tmp->rx_nbits = u_tmp->rx_nbits;
  231. k_tmp->bits_per_word = u_tmp->bits_per_word;
  232. k_tmp->delay_usecs = u_tmp->delay_usecs;
  233. k_tmp->speed_hz = u_tmp->speed_hz;
  234. #ifdef VERBOSE
  235. dev_dbg(&spidev->spi->dev,
  236. " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
  237. u_tmp->len,
  238. u_tmp->rx_buf ? "rx " : "",
  239. u_tmp->tx_buf ? "tx " : "",
  240. u_tmp->cs_change ? "cs " : "",
  241. u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
  242. u_tmp->delay_usecs,
  243. u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
  244. #endif
  245. spi_message_add_tail(k_tmp, &msg);
  246. }
  247. status = spidev_sync(spidev, &msg);
  248. if (status < 0)
  249. goto done;
  250. /* copy any rx data out of bounce buffer */
  251. buf = spidev->buffer;
  252. for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
  253. if (u_tmp->rx_buf) {
  254. if (__copy_to_user((u8 __user *)
  255. (uintptr_t) u_tmp->rx_buf, buf,
  256. u_tmp->len)) {
  257. status = -EFAULT;
  258. goto done;
  259. }
  260. }
  261. buf += u_tmp->len;
  262. }
  263. status = total;
  264. done:
  265. kfree(k_xfers);
  266. return status;
  267. }
  268. static long
  269. spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  270. {
  271. int err = 0;
  272. int retval = 0;
  273. struct spidev_data *spidev;
  274. struct spi_device *spi;
  275. u32 tmp;
  276. unsigned n_ioc;
  277. struct spi_ioc_transfer *ioc;
  278. /* Check type and command number */
  279. if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
  280. return -ENOTTY;
  281. /* Check access direction once here; don't repeat below.
  282. * IOC_DIR is from the user perspective, while access_ok is
  283. * from the kernel perspective; so they look reversed.
  284. */
  285. if (_IOC_DIR(cmd) & _IOC_READ)
  286. err = !access_ok(VERIFY_WRITE,
  287. (void __user *)arg, _IOC_SIZE(cmd));
  288. if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
  289. err = !access_ok(VERIFY_READ,
  290. (void __user *)arg, _IOC_SIZE(cmd));
  291. if (err)
  292. return -EFAULT;
  293. /* guard against device removal before, or while,
  294. * we issue this ioctl.
  295. */
  296. spidev = filp->private_data;
  297. spin_lock_irq(&spidev->spi_lock);
  298. spi = spi_dev_get(spidev->spi);
  299. spin_unlock_irq(&spidev->spi_lock);
  300. if (spi == NULL)
  301. return -ESHUTDOWN;
  302. /* use the buffer lock here for triple duty:
  303. * - prevent I/O (from us) so calling spi_setup() is safe;
  304. * - prevent concurrent SPI_IOC_WR_* from morphing
  305. * data fields while SPI_IOC_RD_* reads them;
  306. * - SPI_IOC_MESSAGE needs the buffer locked "normally".
  307. */
  308. mutex_lock(&spidev->buf_lock);
  309. switch (cmd) {
  310. /* read requests */
  311. case SPI_IOC_RD_MODE:
  312. retval = __put_user(spi->mode & SPI_MODE_MASK,
  313. (__u8 __user *)arg);
  314. break;
  315. case SPI_IOC_RD_MODE32:
  316. retval = __put_user(spi->mode & SPI_MODE_MASK,
  317. (__u32 __user *)arg);
  318. break;
  319. case SPI_IOC_RD_LSB_FIRST:
  320. retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
  321. (__u8 __user *)arg);
  322. break;
  323. case SPI_IOC_RD_BITS_PER_WORD:
  324. retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
  325. break;
  326. case SPI_IOC_RD_MAX_SPEED_HZ:
  327. retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
  328. break;
  329. /* write requests */
  330. case SPI_IOC_WR_MODE:
  331. case SPI_IOC_WR_MODE32:
  332. if (cmd == SPI_IOC_WR_MODE)
  333. retval = __get_user(tmp, (u8 __user *)arg);
  334. else
  335. retval = __get_user(tmp, (u32 __user *)arg);
  336. if (retval == 0) {
  337. u32 save = spi->mode;
  338. if (tmp & ~SPI_MODE_MASK) {
  339. retval = -EINVAL;
  340. break;
  341. }
  342. tmp |= spi->mode & ~SPI_MODE_MASK;
  343. spi->mode = (u16)tmp;
  344. retval = spi_setup(spi);
  345. if (retval < 0)
  346. spi->mode = save;
  347. else
  348. dev_dbg(&spi->dev, "spi mode %x\n", tmp);
  349. }
  350. break;
  351. case SPI_IOC_WR_LSB_FIRST:
  352. retval = __get_user(tmp, (__u8 __user *)arg);
  353. if (retval == 0) {
  354. u32 save = spi->mode;
  355. if (tmp)
  356. spi->mode |= SPI_LSB_FIRST;
  357. else
  358. spi->mode &= ~SPI_LSB_FIRST;
  359. retval = spi_setup(spi);
  360. if (retval < 0)
  361. spi->mode = save;
  362. else
  363. dev_dbg(&spi->dev, "%csb first\n",
  364. tmp ? 'l' : 'm');
  365. }
  366. break;
  367. case SPI_IOC_WR_BITS_PER_WORD:
  368. retval = __get_user(tmp, (__u8 __user *)arg);
  369. if (retval == 0) {
  370. u8 save = spi->bits_per_word;
  371. spi->bits_per_word = tmp;
  372. retval = spi_setup(spi);
  373. if (retval < 0)
  374. spi->bits_per_word = save;
  375. else
  376. dev_dbg(&spi->dev, "%d bits per word\n", tmp);
  377. }
  378. break;
  379. case SPI_IOC_WR_MAX_SPEED_HZ:
  380. retval = __get_user(tmp, (__u32 __user *)arg);
  381. if (retval == 0) {
  382. u32 save = spi->max_speed_hz;
  383. spi->max_speed_hz = tmp;
  384. retval = spi_setup(spi);
  385. if (retval < 0)
  386. spi->max_speed_hz = save;
  387. else
  388. dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
  389. }
  390. break;
  391. default:
  392. /* segmented and/or full-duplex I/O request */
  393. if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
  394. || _IOC_DIR(cmd) != _IOC_WRITE) {
  395. retval = -ENOTTY;
  396. break;
  397. }
  398. tmp = _IOC_SIZE(cmd);
  399. if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
  400. retval = -EINVAL;
  401. break;
  402. }
  403. n_ioc = tmp / sizeof(struct spi_ioc_transfer);
  404. if (n_ioc == 0)
  405. break;
  406. /* copy into scratch area */
  407. ioc = kmalloc(tmp, GFP_KERNEL);
  408. if (!ioc) {
  409. retval = -ENOMEM;
  410. break;
  411. }
  412. if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
  413. kfree(ioc);
  414. retval = -EFAULT;
  415. break;
  416. }
  417. /* translate to spi_message, execute */
  418. retval = spidev_message(spidev, ioc, n_ioc);
  419. kfree(ioc);
  420. break;
  421. }
  422. mutex_unlock(&spidev->buf_lock);
  423. spi_dev_put(spi);
  424. return retval;
  425. }
  426. #ifdef CONFIG_COMPAT
  427. static long
  428. spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  429. {
  430. return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  431. }
  432. #else
  433. #define spidev_compat_ioctl NULL
  434. #endif /* CONFIG_COMPAT */
  435. static int spidev_open(struct inode *inode, struct file *filp)
  436. {
  437. struct spidev_data *spidev;
  438. int status = -ENXIO;
  439. mutex_lock(&device_list_lock);
  440. list_for_each_entry(spidev, &device_list, device_entry) {
  441. if (spidev->devt == inode->i_rdev) {
  442. status = 0;
  443. break;
  444. }
  445. }
  446. if (status == 0) {
  447. if (!spidev->buffer) {
  448. spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
  449. if (!spidev->buffer) {
  450. dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
  451. status = -ENOMEM;
  452. }
  453. }
  454. if (status == 0) {
  455. spidev->users++;
  456. filp->private_data = spidev;
  457. nonseekable_open(inode, filp);
  458. }
  459. } else
  460. pr_debug("spidev: nothing for minor %d\n", iminor(inode));
  461. mutex_unlock(&device_list_lock);
  462. return status;
  463. }
  464. static int spidev_release(struct inode *inode, struct file *filp)
  465. {
  466. struct spidev_data *spidev;
  467. int status = 0;
  468. mutex_lock(&device_list_lock);
  469. spidev = filp->private_data;
  470. filp->private_data = NULL;
  471. /* last close? */
  472. spidev->users--;
  473. if (!spidev->users) {
  474. int dofree;
  475. kfree(spidev->buffer);
  476. spidev->buffer = NULL;
  477. /* ... after we unbound from the underlying device? */
  478. spin_lock_irq(&spidev->spi_lock);
  479. dofree = (spidev->spi == NULL);
  480. spin_unlock_irq(&spidev->spi_lock);
  481. if (dofree)
  482. kfree(spidev);
  483. }
  484. mutex_unlock(&device_list_lock);
  485. return status;
  486. }
  487. static const struct file_operations spidev_fops = {
  488. .owner = THIS_MODULE,
  489. /* REVISIT switch to aio primitives, so that userspace
  490. * gets more complete API coverage. It'll simplify things
  491. * too, except for the locking.
  492. */
  493. .write = spidev_write,
  494. .read = spidev_read,
  495. .unlocked_ioctl = spidev_ioctl,
  496. .compat_ioctl = spidev_compat_ioctl,
  497. .open = spidev_open,
  498. .release = spidev_release,
  499. .llseek = no_llseek,
  500. };
  501. /*-------------------------------------------------------------------------*/
  502. /* The main reason to have this class is to make mdev/udev create the
  503. * /dev/spidevB.C character device nodes exposing our userspace API.
  504. * It also simplifies memory management.
  505. */
  506. static struct class *spidev_class;
  507. /*-------------------------------------------------------------------------*/
  508. static int spidev_probe(struct spi_device *spi)
  509. {
  510. struct spidev_data *spidev;
  511. int status;
  512. unsigned long minor;
  513. /* Allocate driver data */
  514. spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
  515. if (!spidev)
  516. return -ENOMEM;
  517. /* Initialize the driver data */
  518. spidev->spi = spi;
  519. spin_lock_init(&spidev->spi_lock);
  520. mutex_init(&spidev->buf_lock);
  521. INIT_LIST_HEAD(&spidev->device_entry);
  522. /* If we can allocate a minor number, hook up this device.
  523. * Reusing minors is fine so long as udev or mdev is working.
  524. */
  525. mutex_lock(&device_list_lock);
  526. minor = find_first_zero_bit(minors, N_SPI_MINORS);
  527. if (minor < N_SPI_MINORS) {
  528. struct device *dev;
  529. spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
  530. dev = device_create(spidev_class, &spi->dev, spidev->devt,
  531. spidev, "spidev%d.%d",
  532. spi->master->bus_num, spi->chip_select);
  533. status = PTR_ERR_OR_ZERO(dev);
  534. } else {
  535. dev_dbg(&spi->dev, "no minor number available!\n");
  536. status = -ENODEV;
  537. }
  538. if (status == 0) {
  539. set_bit(minor, minors);
  540. list_add(&spidev->device_entry, &device_list);
  541. }
  542. mutex_unlock(&device_list_lock);
  543. if (status == 0)
  544. spi_set_drvdata(spi, spidev);
  545. else
  546. kfree(spidev);
  547. return status;
  548. }
  549. static int spidev_remove(struct spi_device *spi)
  550. {
  551. struct spidev_data *spidev = spi_get_drvdata(spi);
  552. /* make sure ops on existing fds can abort cleanly */
  553. spin_lock_irq(&spidev->spi_lock);
  554. spidev->spi = NULL;
  555. spin_unlock_irq(&spidev->spi_lock);
  556. /* prevent new opens */
  557. mutex_lock(&device_list_lock);
  558. list_del(&spidev->device_entry);
  559. device_destroy(spidev_class, spidev->devt);
  560. clear_bit(MINOR(spidev->devt), minors);
  561. if (spidev->users == 0)
  562. kfree(spidev);
  563. mutex_unlock(&device_list_lock);
  564. return 0;
  565. }
  566. static const struct of_device_id spidev_dt_ids[] = {
  567. { .compatible = "rohm,dh2228fv" },
  568. {},
  569. };
  570. MODULE_DEVICE_TABLE(of, spidev_dt_ids);
  571. static struct spi_driver spidev_spi_driver = {
  572. .driver = {
  573. .name = "spidev",
  574. .owner = THIS_MODULE,
  575. .of_match_table = of_match_ptr(spidev_dt_ids),
  576. },
  577. .probe = spidev_probe,
  578. .remove = spidev_remove,
  579. /* NOTE: suspend/resume methods are not necessary here.
  580. * We don't do anything except pass the requests to/from
  581. * the underlying controller. The refrigerator handles
  582. * most issues; the controller driver handles the rest.
  583. */
  584. };
  585. /*-------------------------------------------------------------------------*/
  586. static int __init spidev_init(void)
  587. {
  588. int status;
  589. /* Claim our 256 reserved device numbers. Then register a class
  590. * that will key udev/mdev to add/remove /dev nodes. Last, register
  591. * the driver which manages those device numbers.
  592. */
  593. BUILD_BUG_ON(N_SPI_MINORS > 256);
  594. status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
  595. if (status < 0)
  596. return status;
  597. spidev_class = class_create(THIS_MODULE, "spidev");
  598. if (IS_ERR(spidev_class)) {
  599. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  600. return PTR_ERR(spidev_class);
  601. }
  602. status = spi_register_driver(&spidev_spi_driver);
  603. if (status < 0) {
  604. class_destroy(spidev_class);
  605. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  606. }
  607. return status;
  608. }
  609. module_init(spidev_init);
  610. static void __exit spidev_exit(void)
  611. {
  612. spi_unregister_driver(&spidev_spi_driver);
  613. class_destroy(spidev_class);
  614. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  615. }
  616. module_exit(spidev_exit);
  617. MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
  618. MODULE_DESCRIPTION("User mode SPI device interface");
  619. MODULE_LICENSE("GPL");
  620. MODULE_ALIAS("spi:spidev");