spi.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*
  2. * SPI init/core code
  3. *
  4. * Copyright (C) 2005 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/device.h>
  22. #include <linux/init.h>
  23. #include <linux/cache.h>
  24. #include <linux/mutex.h>
  25. #include <linux/of_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/mod_devicetable.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/of_spi.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/export.h>
  32. static void spidev_release(struct device *dev)
  33. {
  34. struct spi_device *spi = to_spi_device(dev);
  35. /* spi masters may cleanup for released devices */
  36. if (spi->master->cleanup)
  37. spi->master->cleanup(spi);
  38. spi_master_put(spi->master);
  39. kfree(spi);
  40. }
  41. static ssize_t
  42. modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  43. {
  44. const struct spi_device *spi = to_spi_device(dev);
  45. return sprintf(buf, "%s\n", spi->modalias);
  46. }
  47. static struct device_attribute spi_dev_attrs[] = {
  48. __ATTR_RO(modalias),
  49. __ATTR_NULL,
  50. };
  51. /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
  52. * and the sysfs version makes coldplug work too.
  53. */
  54. static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
  55. const struct spi_device *sdev)
  56. {
  57. while (id->name[0]) {
  58. if (!strcmp(sdev->modalias, id->name))
  59. return id;
  60. id++;
  61. }
  62. return NULL;
  63. }
  64. const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
  65. {
  66. const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
  67. return spi_match_id(sdrv->id_table, sdev);
  68. }
  69. EXPORT_SYMBOL_GPL(spi_get_device_id);
  70. static int spi_match_device(struct device *dev, struct device_driver *drv)
  71. {
  72. const struct spi_device *spi = to_spi_device(dev);
  73. const struct spi_driver *sdrv = to_spi_driver(drv);
  74. /* Attempt an OF style match */
  75. if (of_driver_match_device(dev, drv))
  76. return 1;
  77. if (sdrv->id_table)
  78. return !!spi_match_id(sdrv->id_table, spi);
  79. return strcmp(spi->modalias, drv->name) == 0;
  80. }
  81. static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
  82. {
  83. const struct spi_device *spi = to_spi_device(dev);
  84. add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
  85. return 0;
  86. }
  87. #ifdef CONFIG_PM_SLEEP
  88. static int spi_legacy_suspend(struct device *dev, pm_message_t message)
  89. {
  90. int value = 0;
  91. struct spi_driver *drv = to_spi_driver(dev->driver);
  92. /* suspend will stop irqs and dma; no more i/o */
  93. if (drv) {
  94. if (drv->suspend)
  95. value = drv->suspend(to_spi_device(dev), message);
  96. else
  97. dev_dbg(dev, "... can't suspend\n");
  98. }
  99. return value;
  100. }
  101. static int spi_legacy_resume(struct device *dev)
  102. {
  103. int value = 0;
  104. struct spi_driver *drv = to_spi_driver(dev->driver);
  105. /* resume may restart the i/o queue */
  106. if (drv) {
  107. if (drv->resume)
  108. value = drv->resume(to_spi_device(dev));
  109. else
  110. dev_dbg(dev, "... can't resume\n");
  111. }
  112. return value;
  113. }
  114. static int spi_pm_suspend(struct device *dev)
  115. {
  116. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  117. if (pm)
  118. return pm_generic_suspend(dev);
  119. else
  120. return spi_legacy_suspend(dev, PMSG_SUSPEND);
  121. }
  122. static int spi_pm_resume(struct device *dev)
  123. {
  124. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  125. if (pm)
  126. return pm_generic_resume(dev);
  127. else
  128. return spi_legacy_resume(dev);
  129. }
  130. static int spi_pm_freeze(struct device *dev)
  131. {
  132. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  133. if (pm)
  134. return pm_generic_freeze(dev);
  135. else
  136. return spi_legacy_suspend(dev, PMSG_FREEZE);
  137. }
  138. static int spi_pm_thaw(struct device *dev)
  139. {
  140. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  141. if (pm)
  142. return pm_generic_thaw(dev);
  143. else
  144. return spi_legacy_resume(dev);
  145. }
  146. static int spi_pm_poweroff(struct device *dev)
  147. {
  148. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  149. if (pm)
  150. return pm_generic_poweroff(dev);
  151. else
  152. return spi_legacy_suspend(dev, PMSG_HIBERNATE);
  153. }
  154. static int spi_pm_restore(struct device *dev)
  155. {
  156. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  157. if (pm)
  158. return pm_generic_restore(dev);
  159. else
  160. return spi_legacy_resume(dev);
  161. }
  162. #else
  163. #define spi_pm_suspend NULL
  164. #define spi_pm_resume NULL
  165. #define spi_pm_freeze NULL
  166. #define spi_pm_thaw NULL
  167. #define spi_pm_poweroff NULL
  168. #define spi_pm_restore NULL
  169. #endif
  170. static const struct dev_pm_ops spi_pm = {
  171. .suspend = spi_pm_suspend,
  172. .resume = spi_pm_resume,
  173. .freeze = spi_pm_freeze,
  174. .thaw = spi_pm_thaw,
  175. .poweroff = spi_pm_poweroff,
  176. .restore = spi_pm_restore,
  177. SET_RUNTIME_PM_OPS(
  178. pm_generic_runtime_suspend,
  179. pm_generic_runtime_resume,
  180. pm_generic_runtime_idle
  181. )
  182. };
  183. struct bus_type spi_bus_type = {
  184. .name = "spi",
  185. .dev_attrs = spi_dev_attrs,
  186. .match = spi_match_device,
  187. .uevent = spi_uevent,
  188. .pm = &spi_pm,
  189. };
  190. EXPORT_SYMBOL_GPL(spi_bus_type);
  191. static int spi_drv_probe(struct device *dev)
  192. {
  193. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  194. return sdrv->probe(to_spi_device(dev));
  195. }
  196. static int spi_drv_remove(struct device *dev)
  197. {
  198. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  199. return sdrv->remove(to_spi_device(dev));
  200. }
  201. static void spi_drv_shutdown(struct device *dev)
  202. {
  203. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  204. sdrv->shutdown(to_spi_device(dev));
  205. }
  206. /**
  207. * spi_register_driver - register a SPI driver
  208. * @sdrv: the driver to register
  209. * Context: can sleep
  210. */
  211. int spi_register_driver(struct spi_driver *sdrv)
  212. {
  213. sdrv->driver.bus = &spi_bus_type;
  214. if (sdrv->probe)
  215. sdrv->driver.probe = spi_drv_probe;
  216. if (sdrv->remove)
  217. sdrv->driver.remove = spi_drv_remove;
  218. if (sdrv->shutdown)
  219. sdrv->driver.shutdown = spi_drv_shutdown;
  220. return driver_register(&sdrv->driver);
  221. }
  222. EXPORT_SYMBOL_GPL(spi_register_driver);
  223. /*-------------------------------------------------------------------------*/
  224. /* SPI devices should normally not be created by SPI device drivers; that
  225. * would make them board-specific. Similarly with SPI master drivers.
  226. * Device registration normally goes into like arch/.../mach.../board-YYY.c
  227. * with other readonly (flashable) information about mainboard devices.
  228. */
  229. struct boardinfo {
  230. struct list_head list;
  231. struct spi_board_info board_info;
  232. };
  233. static LIST_HEAD(board_list);
  234. static LIST_HEAD(spi_master_list);
  235. /*
  236. * Used to protect add/del opertion for board_info list and
  237. * spi_master list, and their matching process
  238. */
  239. static DEFINE_MUTEX(board_lock);
  240. /**
  241. * spi_alloc_device - Allocate a new SPI device
  242. * @master: Controller to which device is connected
  243. * Context: can sleep
  244. *
  245. * Allows a driver to allocate and initialize a spi_device without
  246. * registering it immediately. This allows a driver to directly
  247. * fill the spi_device with device parameters before calling
  248. * spi_add_device() on it.
  249. *
  250. * Caller is responsible to call spi_add_device() on the returned
  251. * spi_device structure to add it to the SPI master. If the caller
  252. * needs to discard the spi_device without adding it, then it should
  253. * call spi_dev_put() on it.
  254. *
  255. * Returns a pointer to the new device, or NULL.
  256. */
  257. struct spi_device *spi_alloc_device(struct spi_master *master)
  258. {
  259. struct spi_device *spi;
  260. struct device *dev = master->dev.parent;
  261. if (!spi_master_get(master))
  262. return NULL;
  263. spi = kzalloc(sizeof *spi, GFP_KERNEL);
  264. if (!spi) {
  265. dev_err(dev, "cannot alloc spi_device\n");
  266. spi_master_put(master);
  267. return NULL;
  268. }
  269. spi->master = master;
  270. spi->dev.parent = dev;
  271. spi->dev.bus = &spi_bus_type;
  272. spi->dev.release = spidev_release;
  273. device_initialize(&spi->dev);
  274. return spi;
  275. }
  276. EXPORT_SYMBOL_GPL(spi_alloc_device);
  277. /**
  278. * spi_add_device - Add spi_device allocated with spi_alloc_device
  279. * @spi: spi_device to register
  280. *
  281. * Companion function to spi_alloc_device. Devices allocated with
  282. * spi_alloc_device can be added onto the spi bus with this function.
  283. *
  284. * Returns 0 on success; negative errno on failure
  285. */
  286. int spi_add_device(struct spi_device *spi)
  287. {
  288. static DEFINE_MUTEX(spi_add_lock);
  289. struct device *dev = spi->master->dev.parent;
  290. struct device *d;
  291. int status;
  292. /* Chipselects are numbered 0..max; validate. */
  293. if (spi->chip_select >= spi->master->num_chipselect) {
  294. dev_err(dev, "cs%d >= max %d\n",
  295. spi->chip_select,
  296. spi->master->num_chipselect);
  297. return -EINVAL;
  298. }
  299. /* Set the bus ID string */
  300. dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
  301. spi->chip_select);
  302. /* We need to make sure there's no other device with this
  303. * chipselect **BEFORE** we call setup(), else we'll trash
  304. * its configuration. Lock against concurrent add() calls.
  305. */
  306. mutex_lock(&spi_add_lock);
  307. d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
  308. if (d != NULL) {
  309. dev_err(dev, "chipselect %d already in use\n",
  310. spi->chip_select);
  311. put_device(d);
  312. status = -EBUSY;
  313. goto done;
  314. }
  315. /* Drivers may modify this initial i/o setup, but will
  316. * normally rely on the device being setup. Devices
  317. * using SPI_CS_HIGH can't coexist well otherwise...
  318. */
  319. status = spi_setup(spi);
  320. if (status < 0) {
  321. dev_err(dev, "can't setup %s, status %d\n",
  322. dev_name(&spi->dev), status);
  323. goto done;
  324. }
  325. /* Device may be bound to an active driver when this returns */
  326. status = device_add(&spi->dev);
  327. if (status < 0)
  328. dev_err(dev, "can't add %s, status %d\n",
  329. dev_name(&spi->dev), status);
  330. else
  331. dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
  332. done:
  333. mutex_unlock(&spi_add_lock);
  334. return status;
  335. }
  336. EXPORT_SYMBOL_GPL(spi_add_device);
  337. /**
  338. * spi_new_device - instantiate one new SPI device
  339. * @master: Controller to which device is connected
  340. * @chip: Describes the SPI device
  341. * Context: can sleep
  342. *
  343. * On typical mainboards, this is purely internal; and it's not needed
  344. * after board init creates the hard-wired devices. Some development
  345. * platforms may not be able to use spi_register_board_info though, and
  346. * this is exported so that for example a USB or parport based adapter
  347. * driver could add devices (which it would learn about out-of-band).
  348. *
  349. * Returns the new device, or NULL.
  350. */
  351. struct spi_device *spi_new_device(struct spi_master *master,
  352. struct spi_board_info *chip)
  353. {
  354. struct spi_device *proxy;
  355. int status;
  356. /* NOTE: caller did any chip->bus_num checks necessary.
  357. *
  358. * Also, unless we change the return value convention to use
  359. * error-or-pointer (not NULL-or-pointer), troubleshootability
  360. * suggests syslogged diagnostics are best here (ugh).
  361. */
  362. proxy = spi_alloc_device(master);
  363. if (!proxy)
  364. return NULL;
  365. WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
  366. proxy->chip_select = chip->chip_select;
  367. proxy->max_speed_hz = chip->max_speed_hz;
  368. proxy->mode = chip->mode;
  369. proxy->irq = chip->irq;
  370. strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
  371. proxy->dev.platform_data = (void *) chip->platform_data;
  372. proxy->controller_data = chip->controller_data;
  373. proxy->controller_state = NULL;
  374. status = spi_add_device(proxy);
  375. if (status < 0) {
  376. spi_dev_put(proxy);
  377. return NULL;
  378. }
  379. return proxy;
  380. }
  381. EXPORT_SYMBOL_GPL(spi_new_device);
  382. static void spi_match_master_to_boardinfo(struct spi_master *master,
  383. struct spi_board_info *bi)
  384. {
  385. struct spi_device *dev;
  386. if (master->bus_num != bi->bus_num)
  387. return;
  388. dev = spi_new_device(master, bi);
  389. if (!dev)
  390. dev_err(master->dev.parent, "can't create new device for %s\n",
  391. bi->modalias);
  392. }
  393. /**
  394. * spi_register_board_info - register SPI devices for a given board
  395. * @info: array of chip descriptors
  396. * @n: how many descriptors are provided
  397. * Context: can sleep
  398. *
  399. * Board-specific early init code calls this (probably during arch_initcall)
  400. * with segments of the SPI device table. Any device nodes are created later,
  401. * after the relevant parent SPI controller (bus_num) is defined. We keep
  402. * this table of devices forever, so that reloading a controller driver will
  403. * not make Linux forget about these hard-wired devices.
  404. *
  405. * Other code can also call this, e.g. a particular add-on board might provide
  406. * SPI devices through its expansion connector, so code initializing that board
  407. * would naturally declare its SPI devices.
  408. *
  409. * The board info passed can safely be __initdata ... but be careful of
  410. * any embedded pointers (platform_data, etc), they're copied as-is.
  411. */
  412. int __init
  413. spi_register_board_info(struct spi_board_info const *info, unsigned n)
  414. {
  415. struct boardinfo *bi;
  416. int i;
  417. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  418. if (!bi)
  419. return -ENOMEM;
  420. for (i = 0; i < n; i++, bi++, info++) {
  421. struct spi_master *master;
  422. memcpy(&bi->board_info, info, sizeof(*info));
  423. mutex_lock(&board_lock);
  424. list_add_tail(&bi->list, &board_list);
  425. list_for_each_entry(master, &spi_master_list, list)
  426. spi_match_master_to_boardinfo(master, &bi->board_info);
  427. mutex_unlock(&board_lock);
  428. }
  429. return 0;
  430. }
  431. /*-------------------------------------------------------------------------*/
  432. static void spi_master_release(struct device *dev)
  433. {
  434. struct spi_master *master;
  435. master = container_of(dev, struct spi_master, dev);
  436. kfree(master);
  437. }
  438. static struct class spi_master_class = {
  439. .name = "spi_master",
  440. .owner = THIS_MODULE,
  441. .dev_release = spi_master_release,
  442. };
  443. /**
  444. * spi_alloc_master - allocate SPI master controller
  445. * @dev: the controller, possibly using the platform_bus
  446. * @size: how much zeroed driver-private data to allocate; the pointer to this
  447. * memory is in the driver_data field of the returned device,
  448. * accessible with spi_master_get_devdata().
  449. * Context: can sleep
  450. *
  451. * This call is used only by SPI master controller drivers, which are the
  452. * only ones directly touching chip registers. It's how they allocate
  453. * an spi_master structure, prior to calling spi_register_master().
  454. *
  455. * This must be called from context that can sleep. It returns the SPI
  456. * master structure on success, else NULL.
  457. *
  458. * The caller is responsible for assigning the bus number and initializing
  459. * the master's methods before calling spi_register_master(); and (after errors
  460. * adding the device) calling spi_master_put() to prevent a memory leak.
  461. */
  462. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  463. {
  464. struct spi_master *master;
  465. if (!dev)
  466. return NULL;
  467. master = kzalloc(size + sizeof *master, GFP_KERNEL);
  468. if (!master)
  469. return NULL;
  470. device_initialize(&master->dev);
  471. master->dev.class = &spi_master_class;
  472. master->dev.parent = get_device(dev);
  473. spi_master_set_devdata(master, &master[1]);
  474. return master;
  475. }
  476. EXPORT_SYMBOL_GPL(spi_alloc_master);
  477. /**
  478. * spi_register_master - register SPI master controller
  479. * @master: initialized master, originally from spi_alloc_master()
  480. * Context: can sleep
  481. *
  482. * SPI master controllers connect to their drivers using some non-SPI bus,
  483. * such as the platform bus. The final stage of probe() in that code
  484. * includes calling spi_register_master() to hook up to this SPI bus glue.
  485. *
  486. * SPI controllers use board specific (often SOC specific) bus numbers,
  487. * and board-specific addressing for SPI devices combines those numbers
  488. * with chip select numbers. Since SPI does not directly support dynamic
  489. * device identification, boards need configuration tables telling which
  490. * chip is at which address.
  491. *
  492. * This must be called from context that can sleep. It returns zero on
  493. * success, else a negative error code (dropping the master's refcount).
  494. * After a successful return, the caller is responsible for calling
  495. * spi_unregister_master().
  496. */
  497. int spi_register_master(struct spi_master *master)
  498. {
  499. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  500. struct device *dev = master->dev.parent;
  501. struct boardinfo *bi;
  502. int status = -ENODEV;
  503. int dynamic = 0;
  504. if (!dev)
  505. return -ENODEV;
  506. /* even if it's just one always-selected device, there must
  507. * be at least one chipselect
  508. */
  509. if (master->num_chipselect == 0)
  510. return -EINVAL;
  511. /* convention: dynamically assigned bus IDs count down from the max */
  512. if (master->bus_num < 0) {
  513. /* FIXME switch to an IDR based scheme, something like
  514. * I2C now uses, so we can't run out of "dynamic" IDs
  515. */
  516. master->bus_num = atomic_dec_return(&dyn_bus_id);
  517. dynamic = 1;
  518. }
  519. spin_lock_init(&master->bus_lock_spinlock);
  520. mutex_init(&master->bus_lock_mutex);
  521. master->bus_lock_flag = 0;
  522. /* register the device, then userspace will see it.
  523. * registration fails if the bus ID is in use.
  524. */
  525. dev_set_name(&master->dev, "spi%u", master->bus_num);
  526. status = device_add(&master->dev);
  527. if (status < 0)
  528. goto done;
  529. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  530. dynamic ? " (dynamic)" : "");
  531. mutex_lock(&board_lock);
  532. list_add_tail(&master->list, &spi_master_list);
  533. list_for_each_entry(bi, &board_list, list)
  534. spi_match_master_to_boardinfo(master, &bi->board_info);
  535. mutex_unlock(&board_lock);
  536. status = 0;
  537. /* Register devices from the device tree */
  538. of_register_spi_devices(master);
  539. done:
  540. return status;
  541. }
  542. EXPORT_SYMBOL_GPL(spi_register_master);
  543. static int __unregister(struct device *dev, void *null)
  544. {
  545. spi_unregister_device(to_spi_device(dev));
  546. return 0;
  547. }
  548. /**
  549. * spi_unregister_master - unregister SPI master controller
  550. * @master: the master being unregistered
  551. * Context: can sleep
  552. *
  553. * This call is used only by SPI master controller drivers, which are the
  554. * only ones directly touching chip registers.
  555. *
  556. * This must be called from context that can sleep.
  557. */
  558. void spi_unregister_master(struct spi_master *master)
  559. {
  560. int dummy;
  561. mutex_lock(&board_lock);
  562. list_del(&master->list);
  563. mutex_unlock(&board_lock);
  564. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  565. device_unregister(&master->dev);
  566. }
  567. EXPORT_SYMBOL_GPL(spi_unregister_master);
  568. static int __spi_master_match(struct device *dev, void *data)
  569. {
  570. struct spi_master *m;
  571. u16 *bus_num = data;
  572. m = container_of(dev, struct spi_master, dev);
  573. return m->bus_num == *bus_num;
  574. }
  575. /**
  576. * spi_busnum_to_master - look up master associated with bus_num
  577. * @bus_num: the master's bus number
  578. * Context: can sleep
  579. *
  580. * This call may be used with devices that are registered after
  581. * arch init time. It returns a refcounted pointer to the relevant
  582. * spi_master (which the caller must release), or NULL if there is
  583. * no such master registered.
  584. */
  585. struct spi_master *spi_busnum_to_master(u16 bus_num)
  586. {
  587. struct device *dev;
  588. struct spi_master *master = NULL;
  589. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  590. __spi_master_match);
  591. if (dev)
  592. master = container_of(dev, struct spi_master, dev);
  593. /* reference got in class_find_device */
  594. return master;
  595. }
  596. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  597. /*-------------------------------------------------------------------------*/
  598. /* Core methods for SPI master protocol drivers. Some of the
  599. * other core methods are currently defined as inline functions.
  600. */
  601. /**
  602. * spi_setup - setup SPI mode and clock rate
  603. * @spi: the device whose settings are being modified
  604. * Context: can sleep, and no requests are queued to the device
  605. *
  606. * SPI protocol drivers may need to update the transfer mode if the
  607. * device doesn't work with its default. They may likewise need
  608. * to update clock rates or word sizes from initial values. This function
  609. * changes those settings, and must be called from a context that can sleep.
  610. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  611. * effect the next time the device is selected and data is transferred to
  612. * or from it. When this function returns, the spi device is deselected.
  613. *
  614. * Note that this call will fail if the protocol driver specifies an option
  615. * that the underlying controller or its driver does not support. For
  616. * example, not all hardware supports wire transfers using nine bit words,
  617. * LSB-first wire encoding, or active-high chipselects.
  618. */
  619. int spi_setup(struct spi_device *spi)
  620. {
  621. unsigned bad_bits;
  622. int status;
  623. /* help drivers fail *cleanly* when they need options
  624. * that aren't supported with their current master
  625. */
  626. bad_bits = spi->mode & ~spi->master->mode_bits;
  627. if (bad_bits) {
  628. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  629. bad_bits);
  630. return -EINVAL;
  631. }
  632. if (!spi->bits_per_word)
  633. spi->bits_per_word = 8;
  634. status = spi->master->setup(spi);
  635. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
  636. "%u bits/w, %u Hz max --> %d\n",
  637. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  638. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  639. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  640. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  641. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  642. spi->bits_per_word, spi->max_speed_hz,
  643. status);
  644. return status;
  645. }
  646. EXPORT_SYMBOL_GPL(spi_setup);
  647. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  648. {
  649. struct spi_master *master = spi->master;
  650. /* Half-duplex links include original MicroWire, and ones with
  651. * only one data pin like SPI_3WIRE (switches direction) or where
  652. * either MOSI or MISO is missing. They can also be caused by
  653. * software limitations.
  654. */
  655. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  656. || (spi->mode & SPI_3WIRE)) {
  657. struct spi_transfer *xfer;
  658. unsigned flags = master->flags;
  659. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  660. if (xfer->rx_buf && xfer->tx_buf)
  661. return -EINVAL;
  662. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  663. return -EINVAL;
  664. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  665. return -EINVAL;
  666. }
  667. }
  668. message->spi = spi;
  669. message->status = -EINPROGRESS;
  670. return master->transfer(spi, message);
  671. }
  672. /**
  673. * spi_async - asynchronous SPI transfer
  674. * @spi: device with which data will be exchanged
  675. * @message: describes the data transfers, including completion callback
  676. * Context: any (irqs may be blocked, etc)
  677. *
  678. * This call may be used in_irq and other contexts which can't sleep,
  679. * as well as from task contexts which can sleep.
  680. *
  681. * The completion callback is invoked in a context which can't sleep.
  682. * Before that invocation, the value of message->status is undefined.
  683. * When the callback is issued, message->status holds either zero (to
  684. * indicate complete success) or a negative error code. After that
  685. * callback returns, the driver which issued the transfer request may
  686. * deallocate the associated memory; it's no longer in use by any SPI
  687. * core or controller driver code.
  688. *
  689. * Note that although all messages to a spi_device are handled in
  690. * FIFO order, messages may go to different devices in other orders.
  691. * Some device might be higher priority, or have various "hard" access
  692. * time requirements, for example.
  693. *
  694. * On detection of any fault during the transfer, processing of
  695. * the entire message is aborted, and the device is deselected.
  696. * Until returning from the associated message completion callback,
  697. * no other spi_message queued to that device will be processed.
  698. * (This rule applies equally to all the synchronous transfer calls,
  699. * which are wrappers around this core asynchronous primitive.)
  700. */
  701. int spi_async(struct spi_device *spi, struct spi_message *message)
  702. {
  703. struct spi_master *master = spi->master;
  704. int ret;
  705. unsigned long flags;
  706. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  707. if (master->bus_lock_flag)
  708. ret = -EBUSY;
  709. else
  710. ret = __spi_async(spi, message);
  711. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  712. return ret;
  713. }
  714. EXPORT_SYMBOL_GPL(spi_async);
  715. /**
  716. * spi_async_locked - version of spi_async with exclusive bus usage
  717. * @spi: device with which data will be exchanged
  718. * @message: describes the data transfers, including completion callback
  719. * Context: any (irqs may be blocked, etc)
  720. *
  721. * This call may be used in_irq and other contexts which can't sleep,
  722. * as well as from task contexts which can sleep.
  723. *
  724. * The completion callback is invoked in a context which can't sleep.
  725. * Before that invocation, the value of message->status is undefined.
  726. * When the callback is issued, message->status holds either zero (to
  727. * indicate complete success) or a negative error code. After that
  728. * callback returns, the driver which issued the transfer request may
  729. * deallocate the associated memory; it's no longer in use by any SPI
  730. * core or controller driver code.
  731. *
  732. * Note that although all messages to a spi_device are handled in
  733. * FIFO order, messages may go to different devices in other orders.
  734. * Some device might be higher priority, or have various "hard" access
  735. * time requirements, for example.
  736. *
  737. * On detection of any fault during the transfer, processing of
  738. * the entire message is aborted, and the device is deselected.
  739. * Until returning from the associated message completion callback,
  740. * no other spi_message queued to that device will be processed.
  741. * (This rule applies equally to all the synchronous transfer calls,
  742. * which are wrappers around this core asynchronous primitive.)
  743. */
  744. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  745. {
  746. struct spi_master *master = spi->master;
  747. int ret;
  748. unsigned long flags;
  749. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  750. ret = __spi_async(spi, message);
  751. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  752. return ret;
  753. }
  754. EXPORT_SYMBOL_GPL(spi_async_locked);
  755. /*-------------------------------------------------------------------------*/
  756. /* Utility methods for SPI master protocol drivers, layered on
  757. * top of the core. Some other utility methods are defined as
  758. * inline functions.
  759. */
  760. static void spi_complete(void *arg)
  761. {
  762. complete(arg);
  763. }
  764. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  765. int bus_locked)
  766. {
  767. DECLARE_COMPLETION_ONSTACK(done);
  768. int status;
  769. struct spi_master *master = spi->master;
  770. message->complete = spi_complete;
  771. message->context = &done;
  772. if (!bus_locked)
  773. mutex_lock(&master->bus_lock_mutex);
  774. status = spi_async_locked(spi, message);
  775. if (!bus_locked)
  776. mutex_unlock(&master->bus_lock_mutex);
  777. if (status == 0) {
  778. wait_for_completion(&done);
  779. status = message->status;
  780. }
  781. message->context = NULL;
  782. return status;
  783. }
  784. /**
  785. * spi_sync - blocking/synchronous SPI data transfers
  786. * @spi: device with which data will be exchanged
  787. * @message: describes the data transfers
  788. * Context: can sleep
  789. *
  790. * This call may only be used from a context that may sleep. The sleep
  791. * is non-interruptible, and has no timeout. Low-overhead controller
  792. * drivers may DMA directly into and out of the message buffers.
  793. *
  794. * Note that the SPI device's chip select is active during the message,
  795. * and then is normally disabled between messages. Drivers for some
  796. * frequently-used devices may want to minimize costs of selecting a chip,
  797. * by leaving it selected in anticipation that the next message will go
  798. * to the same chip. (That may increase power usage.)
  799. *
  800. * Also, the caller is guaranteeing that the memory associated with the
  801. * message will not be freed before this call returns.
  802. *
  803. * It returns zero on success, else a negative error code.
  804. */
  805. int spi_sync(struct spi_device *spi, struct spi_message *message)
  806. {
  807. return __spi_sync(spi, message, 0);
  808. }
  809. EXPORT_SYMBOL_GPL(spi_sync);
  810. /**
  811. * spi_sync_locked - version of spi_sync with exclusive bus usage
  812. * @spi: device with which data will be exchanged
  813. * @message: describes the data transfers
  814. * Context: can sleep
  815. *
  816. * This call may only be used from a context that may sleep. The sleep
  817. * is non-interruptible, and has no timeout. Low-overhead controller
  818. * drivers may DMA directly into and out of the message buffers.
  819. *
  820. * This call should be used by drivers that require exclusive access to the
  821. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  822. * be released by a spi_bus_unlock call when the exclusive access is over.
  823. *
  824. * It returns zero on success, else a negative error code.
  825. */
  826. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  827. {
  828. return __spi_sync(spi, message, 1);
  829. }
  830. EXPORT_SYMBOL_GPL(spi_sync_locked);
  831. /**
  832. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  833. * @master: SPI bus master that should be locked for exclusive bus access
  834. * Context: can sleep
  835. *
  836. * This call may only be used from a context that may sleep. The sleep
  837. * is non-interruptible, and has no timeout.
  838. *
  839. * This call should be used by drivers that require exclusive access to the
  840. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  841. * exclusive access is over. Data transfer must be done by spi_sync_locked
  842. * and spi_async_locked calls when the SPI bus lock is held.
  843. *
  844. * It returns zero on success, else a negative error code.
  845. */
  846. int spi_bus_lock(struct spi_master *master)
  847. {
  848. unsigned long flags;
  849. mutex_lock(&master->bus_lock_mutex);
  850. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  851. master->bus_lock_flag = 1;
  852. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  853. /* mutex remains locked until spi_bus_unlock is called */
  854. return 0;
  855. }
  856. EXPORT_SYMBOL_GPL(spi_bus_lock);
  857. /**
  858. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  859. * @master: SPI bus master that was locked for exclusive bus access
  860. * Context: can sleep
  861. *
  862. * This call may only be used from a context that may sleep. The sleep
  863. * is non-interruptible, and has no timeout.
  864. *
  865. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  866. * call.
  867. *
  868. * It returns zero on success, else a negative error code.
  869. */
  870. int spi_bus_unlock(struct spi_master *master)
  871. {
  872. master->bus_lock_flag = 0;
  873. mutex_unlock(&master->bus_lock_mutex);
  874. return 0;
  875. }
  876. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  877. /* portable code must never pass more than 32 bytes */
  878. #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
  879. static u8 *buf;
  880. /**
  881. * spi_write_then_read - SPI synchronous write followed by read
  882. * @spi: device with which data will be exchanged
  883. * @txbuf: data to be written (need not be dma-safe)
  884. * @n_tx: size of txbuf, in bytes
  885. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  886. * @n_rx: size of rxbuf, in bytes
  887. * Context: can sleep
  888. *
  889. * This performs a half duplex MicroWire style transaction with the
  890. * device, sending txbuf and then reading rxbuf. The return value
  891. * is zero for success, else a negative errno status code.
  892. * This call may only be used from a context that may sleep.
  893. *
  894. * Parameters to this routine are always copied using a small buffer;
  895. * portable code should never use this for more than 32 bytes.
  896. * Performance-sensitive or bulk transfer code should instead use
  897. * spi_{async,sync}() calls with dma-safe buffers.
  898. */
  899. int spi_write_then_read(struct spi_device *spi,
  900. const void *txbuf, unsigned n_tx,
  901. void *rxbuf, unsigned n_rx)
  902. {
  903. static DEFINE_MUTEX(lock);
  904. int status;
  905. struct spi_message message;
  906. struct spi_transfer x[2];
  907. u8 *local_buf;
  908. /* Use preallocated DMA-safe buffer. We can't avoid copying here,
  909. * (as a pure convenience thing), but we can keep heap costs
  910. * out of the hot path ...
  911. */
  912. if ((n_tx + n_rx) > SPI_BUFSIZ)
  913. return -EINVAL;
  914. spi_message_init(&message);
  915. memset(x, 0, sizeof x);
  916. if (n_tx) {
  917. x[0].len = n_tx;
  918. spi_message_add_tail(&x[0], &message);
  919. }
  920. if (n_rx) {
  921. x[1].len = n_rx;
  922. spi_message_add_tail(&x[1], &message);
  923. }
  924. /* ... unless someone else is using the pre-allocated buffer */
  925. if (!mutex_trylock(&lock)) {
  926. local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  927. if (!local_buf)
  928. return -ENOMEM;
  929. } else
  930. local_buf = buf;
  931. memcpy(local_buf, txbuf, n_tx);
  932. x[0].tx_buf = local_buf;
  933. x[1].rx_buf = local_buf + n_tx;
  934. /* do the i/o */
  935. status = spi_sync(spi, &message);
  936. if (status == 0)
  937. memcpy(rxbuf, x[1].rx_buf, n_rx);
  938. if (x[0].tx_buf == buf)
  939. mutex_unlock(&lock);
  940. else
  941. kfree(local_buf);
  942. return status;
  943. }
  944. EXPORT_SYMBOL_GPL(spi_write_then_read);
  945. /*-------------------------------------------------------------------------*/
  946. static int __init spi_init(void)
  947. {
  948. int status;
  949. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  950. if (!buf) {
  951. status = -ENOMEM;
  952. goto err0;
  953. }
  954. status = bus_register(&spi_bus_type);
  955. if (status < 0)
  956. goto err1;
  957. status = class_register(&spi_master_class);
  958. if (status < 0)
  959. goto err2;
  960. return 0;
  961. err2:
  962. bus_unregister(&spi_bus_type);
  963. err1:
  964. kfree(buf);
  965. buf = NULL;
  966. err0:
  967. return status;
  968. }
  969. /* board_info is normally registered in arch_initcall(),
  970. * but even essential drivers wait till later
  971. *
  972. * REVISIT only boardinfo really needs static linking. the rest (device and
  973. * driver registration) _could_ be dynamically linked (modular) ... costs
  974. * include needing to have boardinfo data structures be much more public.
  975. */
  976. postcore_initcall(spi_init);