spi.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. /*
  2. * SPI init/core code
  3. *
  4. * Copyright (C) 2005 David Brownell
  5. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/kmod.h>
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/cache.h>
  26. #include <linux/mutex.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/slab.h>
  30. #include <linux/mod_devicetable.h>
  31. #include <linux/spi/spi.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/export.h>
  35. #include <linux/sched/rt.h>
  36. #include <linux/delay.h>
  37. #include <linux/kthread.h>
  38. #include <linux/ioport.h>
  39. #include <linux/acpi.h>
  40. #define CREATE_TRACE_POINTS
  41. #include <trace/events/spi.h>
  42. static void spidev_release(struct device *dev)
  43. {
  44. struct spi_device *spi = to_spi_device(dev);
  45. /* spi masters may cleanup for released devices */
  46. if (spi->master->cleanup)
  47. spi->master->cleanup(spi);
  48. spi_master_put(spi->master);
  49. kfree(spi);
  50. }
  51. static ssize_t
  52. modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  53. {
  54. const struct spi_device *spi = to_spi_device(dev);
  55. return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
  56. }
  57. static DEVICE_ATTR_RO(modalias);
  58. static struct attribute *spi_dev_attrs[] = {
  59. &dev_attr_modalias.attr,
  60. NULL,
  61. };
  62. ATTRIBUTE_GROUPS(spi_dev);
  63. /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
  64. * and the sysfs version makes coldplug work too.
  65. */
  66. static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
  67. const struct spi_device *sdev)
  68. {
  69. while (id->name[0]) {
  70. if (!strcmp(sdev->modalias, id->name))
  71. return id;
  72. id++;
  73. }
  74. return NULL;
  75. }
  76. const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
  77. {
  78. const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
  79. return spi_match_id(sdrv->id_table, sdev);
  80. }
  81. EXPORT_SYMBOL_GPL(spi_get_device_id);
  82. static int spi_match_device(struct device *dev, struct device_driver *drv)
  83. {
  84. const struct spi_device *spi = to_spi_device(dev);
  85. const struct spi_driver *sdrv = to_spi_driver(drv);
  86. /* Attempt an OF style match */
  87. if (of_driver_match_device(dev, drv))
  88. return 1;
  89. /* Then try ACPI */
  90. if (acpi_driver_match_device(dev, drv))
  91. return 1;
  92. if (sdrv->id_table)
  93. return !!spi_match_id(sdrv->id_table, spi);
  94. return strcmp(spi->modalias, drv->name) == 0;
  95. }
  96. static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
  97. {
  98. const struct spi_device *spi = to_spi_device(dev);
  99. add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
  100. return 0;
  101. }
  102. #ifdef CONFIG_PM_SLEEP
  103. static int spi_legacy_suspend(struct device *dev, pm_message_t message)
  104. {
  105. int value = 0;
  106. struct spi_driver *drv = to_spi_driver(dev->driver);
  107. /* suspend will stop irqs and dma; no more i/o */
  108. if (drv) {
  109. if (drv->suspend)
  110. value = drv->suspend(to_spi_device(dev), message);
  111. else
  112. dev_dbg(dev, "... can't suspend\n");
  113. }
  114. return value;
  115. }
  116. static int spi_legacy_resume(struct device *dev)
  117. {
  118. int value = 0;
  119. struct spi_driver *drv = to_spi_driver(dev->driver);
  120. /* resume may restart the i/o queue */
  121. if (drv) {
  122. if (drv->resume)
  123. value = drv->resume(to_spi_device(dev));
  124. else
  125. dev_dbg(dev, "... can't resume\n");
  126. }
  127. return value;
  128. }
  129. static int spi_pm_suspend(struct device *dev)
  130. {
  131. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  132. if (pm)
  133. return pm_generic_suspend(dev);
  134. else
  135. return spi_legacy_suspend(dev, PMSG_SUSPEND);
  136. }
  137. static int spi_pm_resume(struct device *dev)
  138. {
  139. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  140. if (pm)
  141. return pm_generic_resume(dev);
  142. else
  143. return spi_legacy_resume(dev);
  144. }
  145. static int spi_pm_freeze(struct device *dev)
  146. {
  147. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  148. if (pm)
  149. return pm_generic_freeze(dev);
  150. else
  151. return spi_legacy_suspend(dev, PMSG_FREEZE);
  152. }
  153. static int spi_pm_thaw(struct device *dev)
  154. {
  155. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  156. if (pm)
  157. return pm_generic_thaw(dev);
  158. else
  159. return spi_legacy_resume(dev);
  160. }
  161. static int spi_pm_poweroff(struct device *dev)
  162. {
  163. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  164. if (pm)
  165. return pm_generic_poweroff(dev);
  166. else
  167. return spi_legacy_suspend(dev, PMSG_HIBERNATE);
  168. }
  169. static int spi_pm_restore(struct device *dev)
  170. {
  171. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  172. if (pm)
  173. return pm_generic_restore(dev);
  174. else
  175. return spi_legacy_resume(dev);
  176. }
  177. #else
  178. #define spi_pm_suspend NULL
  179. #define spi_pm_resume NULL
  180. #define spi_pm_freeze NULL
  181. #define spi_pm_thaw NULL
  182. #define spi_pm_poweroff NULL
  183. #define spi_pm_restore NULL
  184. #endif
  185. static const struct dev_pm_ops spi_pm = {
  186. .suspend = spi_pm_suspend,
  187. .resume = spi_pm_resume,
  188. .freeze = spi_pm_freeze,
  189. .thaw = spi_pm_thaw,
  190. .poweroff = spi_pm_poweroff,
  191. .restore = spi_pm_restore,
  192. SET_RUNTIME_PM_OPS(
  193. pm_generic_runtime_suspend,
  194. pm_generic_runtime_resume,
  195. NULL
  196. )
  197. };
  198. struct bus_type spi_bus_type = {
  199. .name = "spi",
  200. .dev_groups = spi_dev_groups,
  201. .match = spi_match_device,
  202. .uevent = spi_uevent,
  203. .pm = &spi_pm,
  204. };
  205. EXPORT_SYMBOL_GPL(spi_bus_type);
  206. static int spi_drv_probe(struct device *dev)
  207. {
  208. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  209. return sdrv->probe(to_spi_device(dev));
  210. }
  211. static int spi_drv_remove(struct device *dev)
  212. {
  213. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  214. return sdrv->remove(to_spi_device(dev));
  215. }
  216. static void spi_drv_shutdown(struct device *dev)
  217. {
  218. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  219. sdrv->shutdown(to_spi_device(dev));
  220. }
  221. /**
  222. * spi_register_driver - register a SPI driver
  223. * @sdrv: the driver to register
  224. * Context: can sleep
  225. */
  226. int spi_register_driver(struct spi_driver *sdrv)
  227. {
  228. sdrv->driver.bus = &spi_bus_type;
  229. if (sdrv->probe)
  230. sdrv->driver.probe = spi_drv_probe;
  231. if (sdrv->remove)
  232. sdrv->driver.remove = spi_drv_remove;
  233. if (sdrv->shutdown)
  234. sdrv->driver.shutdown = spi_drv_shutdown;
  235. return driver_register(&sdrv->driver);
  236. }
  237. EXPORT_SYMBOL_GPL(spi_register_driver);
  238. /*-------------------------------------------------------------------------*/
  239. /* SPI devices should normally not be created by SPI device drivers; that
  240. * would make them board-specific. Similarly with SPI master drivers.
  241. * Device registration normally goes into like arch/.../mach.../board-YYY.c
  242. * with other readonly (flashable) information about mainboard devices.
  243. */
  244. struct boardinfo {
  245. struct list_head list;
  246. struct spi_board_info board_info;
  247. };
  248. static LIST_HEAD(board_list);
  249. static LIST_HEAD(spi_master_list);
  250. /*
  251. * Used to protect add/del opertion for board_info list and
  252. * spi_master list, and their matching process
  253. */
  254. static DEFINE_MUTEX(board_lock);
  255. /**
  256. * spi_alloc_device - Allocate a new SPI device
  257. * @master: Controller to which device is connected
  258. * Context: can sleep
  259. *
  260. * Allows a driver to allocate and initialize a spi_device without
  261. * registering it immediately. This allows a driver to directly
  262. * fill the spi_device with device parameters before calling
  263. * spi_add_device() on it.
  264. *
  265. * Caller is responsible to call spi_add_device() on the returned
  266. * spi_device structure to add it to the SPI master. If the caller
  267. * needs to discard the spi_device without adding it, then it should
  268. * call spi_dev_put() on it.
  269. *
  270. * Returns a pointer to the new device, or NULL.
  271. */
  272. struct spi_device *spi_alloc_device(struct spi_master *master)
  273. {
  274. struct spi_device *spi;
  275. struct device *dev = master->dev.parent;
  276. if (!spi_master_get(master))
  277. return NULL;
  278. spi = kzalloc(sizeof(*spi), GFP_KERNEL);
  279. if (!spi) {
  280. dev_err(dev, "cannot alloc spi_device\n");
  281. spi_master_put(master);
  282. return NULL;
  283. }
  284. spi->master = master;
  285. spi->dev.parent = &master->dev;
  286. spi->dev.bus = &spi_bus_type;
  287. spi->dev.release = spidev_release;
  288. spi->cs_gpio = -ENOENT;
  289. device_initialize(&spi->dev);
  290. return spi;
  291. }
  292. EXPORT_SYMBOL_GPL(spi_alloc_device);
  293. /**
  294. * spi_add_device - Add spi_device allocated with spi_alloc_device
  295. * @spi: spi_device to register
  296. *
  297. * Companion function to spi_alloc_device. Devices allocated with
  298. * spi_alloc_device can be added onto the spi bus with this function.
  299. *
  300. * Returns 0 on success; negative errno on failure
  301. */
  302. int spi_add_device(struct spi_device *spi)
  303. {
  304. static DEFINE_MUTEX(spi_add_lock);
  305. struct spi_master *master = spi->master;
  306. struct device *dev = master->dev.parent;
  307. struct device *d;
  308. int status;
  309. /* Chipselects are numbered 0..max; validate. */
  310. if (spi->chip_select >= master->num_chipselect) {
  311. dev_err(dev, "cs%d >= max %d\n",
  312. spi->chip_select,
  313. master->num_chipselect);
  314. return -EINVAL;
  315. }
  316. /* Set the bus ID string */
  317. dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
  318. spi->chip_select);
  319. /* We need to make sure there's no other device with this
  320. * chipselect **BEFORE** we call setup(), else we'll trash
  321. * its configuration. Lock against concurrent add() calls.
  322. */
  323. mutex_lock(&spi_add_lock);
  324. d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
  325. if (d != NULL) {
  326. dev_err(dev, "chipselect %d already in use\n",
  327. spi->chip_select);
  328. put_device(d);
  329. status = -EBUSY;
  330. goto done;
  331. }
  332. if (master->cs_gpios)
  333. spi->cs_gpio = master->cs_gpios[spi->chip_select];
  334. /* Drivers may modify this initial i/o setup, but will
  335. * normally rely on the device being setup. Devices
  336. * using SPI_CS_HIGH can't coexist well otherwise...
  337. */
  338. status = spi_setup(spi);
  339. if (status < 0) {
  340. dev_err(dev, "can't setup %s, status %d\n",
  341. dev_name(&spi->dev), status);
  342. goto done;
  343. }
  344. /* Device may be bound to an active driver when this returns */
  345. status = device_add(&spi->dev);
  346. if (status < 0)
  347. dev_err(dev, "can't add %s, status %d\n",
  348. dev_name(&spi->dev), status);
  349. else
  350. dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
  351. done:
  352. mutex_unlock(&spi_add_lock);
  353. return status;
  354. }
  355. EXPORT_SYMBOL_GPL(spi_add_device);
  356. /**
  357. * spi_new_device - instantiate one new SPI device
  358. * @master: Controller to which device is connected
  359. * @chip: Describes the SPI device
  360. * Context: can sleep
  361. *
  362. * On typical mainboards, this is purely internal; and it's not needed
  363. * after board init creates the hard-wired devices. Some development
  364. * platforms may not be able to use spi_register_board_info though, and
  365. * this is exported so that for example a USB or parport based adapter
  366. * driver could add devices (which it would learn about out-of-band).
  367. *
  368. * Returns the new device, or NULL.
  369. */
  370. struct spi_device *spi_new_device(struct spi_master *master,
  371. struct spi_board_info *chip)
  372. {
  373. struct spi_device *proxy;
  374. int status;
  375. /* NOTE: caller did any chip->bus_num checks necessary.
  376. *
  377. * Also, unless we change the return value convention to use
  378. * error-or-pointer (not NULL-or-pointer), troubleshootability
  379. * suggests syslogged diagnostics are best here (ugh).
  380. */
  381. proxy = spi_alloc_device(master);
  382. if (!proxy)
  383. return NULL;
  384. WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
  385. proxy->chip_select = chip->chip_select;
  386. proxy->max_speed_hz = chip->max_speed_hz;
  387. proxy->mode = chip->mode;
  388. proxy->irq = chip->irq;
  389. strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
  390. proxy->dev.platform_data = (void *) chip->platform_data;
  391. proxy->controller_data = chip->controller_data;
  392. proxy->controller_state = NULL;
  393. status = spi_add_device(proxy);
  394. if (status < 0) {
  395. spi_dev_put(proxy);
  396. return NULL;
  397. }
  398. return proxy;
  399. }
  400. EXPORT_SYMBOL_GPL(spi_new_device);
  401. static void spi_match_master_to_boardinfo(struct spi_master *master,
  402. struct spi_board_info *bi)
  403. {
  404. struct spi_device *dev;
  405. if (master->bus_num != bi->bus_num)
  406. return;
  407. dev = spi_new_device(master, bi);
  408. if (!dev)
  409. dev_err(master->dev.parent, "can't create new device for %s\n",
  410. bi->modalias);
  411. }
  412. /**
  413. * spi_register_board_info - register SPI devices for a given board
  414. * @info: array of chip descriptors
  415. * @n: how many descriptors are provided
  416. * Context: can sleep
  417. *
  418. * Board-specific early init code calls this (probably during arch_initcall)
  419. * with segments of the SPI device table. Any device nodes are created later,
  420. * after the relevant parent SPI controller (bus_num) is defined. We keep
  421. * this table of devices forever, so that reloading a controller driver will
  422. * not make Linux forget about these hard-wired devices.
  423. *
  424. * Other code can also call this, e.g. a particular add-on board might provide
  425. * SPI devices through its expansion connector, so code initializing that board
  426. * would naturally declare its SPI devices.
  427. *
  428. * The board info passed can safely be __initdata ... but be careful of
  429. * any embedded pointers (platform_data, etc), they're copied as-is.
  430. */
  431. int spi_register_board_info(struct spi_board_info const *info, unsigned n)
  432. {
  433. struct boardinfo *bi;
  434. int i;
  435. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  436. if (!bi)
  437. return -ENOMEM;
  438. for (i = 0; i < n; i++, bi++, info++) {
  439. struct spi_master *master;
  440. memcpy(&bi->board_info, info, sizeof(*info));
  441. mutex_lock(&board_lock);
  442. list_add_tail(&bi->list, &board_list);
  443. list_for_each_entry(master, &spi_master_list, list)
  444. spi_match_master_to_boardinfo(master, &bi->board_info);
  445. mutex_unlock(&board_lock);
  446. }
  447. return 0;
  448. }
  449. /*-------------------------------------------------------------------------*/
  450. static void spi_set_cs(struct spi_device *spi, bool enable)
  451. {
  452. if (spi->mode & SPI_CS_HIGH)
  453. enable = !enable;
  454. if (spi->cs_gpio >= 0)
  455. gpio_set_value(spi->cs_gpio, !enable);
  456. else if (spi->master->set_cs)
  457. spi->master->set_cs(spi, !enable);
  458. }
  459. /*
  460. * spi_transfer_one_message - Default implementation of transfer_one_message()
  461. *
  462. * This is a standard implementation of transfer_one_message() for
  463. * drivers which impelment a transfer_one() operation. It provides
  464. * standard handling of delays and chip select management.
  465. */
  466. static int spi_transfer_one_message(struct spi_master *master,
  467. struct spi_message *msg)
  468. {
  469. struct spi_transfer *xfer;
  470. bool cur_cs = true;
  471. bool keep_cs = false;
  472. int ret = 0;
  473. spi_set_cs(msg->spi, true);
  474. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  475. trace_spi_transfer_start(msg, xfer);
  476. INIT_COMPLETION(master->xfer_completion);
  477. ret = master->transfer_one(master, msg->spi, xfer);
  478. if (ret < 0) {
  479. dev_err(&msg->spi->dev,
  480. "SPI transfer failed: %d\n", ret);
  481. goto out;
  482. }
  483. if (ret > 0)
  484. wait_for_completion(&master->xfer_completion);
  485. trace_spi_transfer_stop(msg, xfer);
  486. if (msg->status != -EINPROGRESS)
  487. goto out;
  488. if (xfer->delay_usecs)
  489. udelay(xfer->delay_usecs);
  490. if (xfer->cs_change) {
  491. if (list_is_last(&xfer->transfer_list,
  492. &msg->transfers)) {
  493. keep_cs = true;
  494. } else {
  495. cur_cs = !cur_cs;
  496. spi_set_cs(msg->spi, cur_cs);
  497. }
  498. }
  499. msg->actual_length += xfer->len;
  500. }
  501. out:
  502. if (ret != 0 || !keep_cs)
  503. spi_set_cs(msg->spi, false);
  504. if (msg->status == -EINPROGRESS)
  505. msg->status = ret;
  506. spi_finalize_current_message(master);
  507. return ret;
  508. }
  509. /**
  510. * spi_finalize_current_transfer - report completion of a transfer
  511. *
  512. * Called by SPI drivers using the core transfer_one_message()
  513. * implementation to notify it that the current interrupt driven
  514. * transfer has finised and the next one may be scheduled.
  515. */
  516. void spi_finalize_current_transfer(struct spi_master *master)
  517. {
  518. complete(&master->xfer_completion);
  519. }
  520. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  521. /**
  522. * spi_pump_messages - kthread work function which processes spi message queue
  523. * @work: pointer to kthread work struct contained in the master struct
  524. *
  525. * This function checks if there is any spi message in the queue that
  526. * needs processing and if so call out to the driver to initialize hardware
  527. * and transfer each message.
  528. *
  529. */
  530. static void spi_pump_messages(struct kthread_work *work)
  531. {
  532. struct spi_master *master =
  533. container_of(work, struct spi_master, pump_messages);
  534. unsigned long flags;
  535. bool was_busy = false;
  536. int ret;
  537. /* Lock queue and check for queue work */
  538. spin_lock_irqsave(&master->queue_lock, flags);
  539. if (list_empty(&master->queue) || !master->running) {
  540. if (!master->busy) {
  541. spin_unlock_irqrestore(&master->queue_lock, flags);
  542. return;
  543. }
  544. master->busy = false;
  545. spin_unlock_irqrestore(&master->queue_lock, flags);
  546. if (master->unprepare_transfer_hardware &&
  547. master->unprepare_transfer_hardware(master))
  548. dev_err(&master->dev,
  549. "failed to unprepare transfer hardware\n");
  550. if (master->auto_runtime_pm) {
  551. pm_runtime_mark_last_busy(master->dev.parent);
  552. pm_runtime_put_autosuspend(master->dev.parent);
  553. }
  554. trace_spi_master_idle(master);
  555. return;
  556. }
  557. /* Make sure we are not already running a message */
  558. if (master->cur_msg) {
  559. spin_unlock_irqrestore(&master->queue_lock, flags);
  560. return;
  561. }
  562. /* Extract head of queue */
  563. master->cur_msg =
  564. list_entry(master->queue.next, struct spi_message, queue);
  565. list_del_init(&master->cur_msg->queue);
  566. if (master->busy)
  567. was_busy = true;
  568. else
  569. master->busy = true;
  570. spin_unlock_irqrestore(&master->queue_lock, flags);
  571. if (!was_busy && master->auto_runtime_pm) {
  572. ret = pm_runtime_get_sync(master->dev.parent);
  573. if (ret < 0) {
  574. dev_err(&master->dev, "Failed to power device: %d\n",
  575. ret);
  576. return;
  577. }
  578. }
  579. if (!was_busy)
  580. trace_spi_master_busy(master);
  581. if (!was_busy && master->prepare_transfer_hardware) {
  582. ret = master->prepare_transfer_hardware(master);
  583. if (ret) {
  584. dev_err(&master->dev,
  585. "failed to prepare transfer hardware\n");
  586. if (master->auto_runtime_pm)
  587. pm_runtime_put(master->dev.parent);
  588. return;
  589. }
  590. }
  591. trace_spi_message_start(master->cur_msg);
  592. if (master->prepare_message) {
  593. ret = master->prepare_message(master, master->cur_msg);
  594. if (ret) {
  595. dev_err(&master->dev,
  596. "failed to prepare message: %d\n", ret);
  597. master->cur_msg->status = ret;
  598. spi_finalize_current_message(master);
  599. return;
  600. }
  601. master->cur_msg_prepared = true;
  602. }
  603. ret = master->transfer_one_message(master, master->cur_msg);
  604. if (ret) {
  605. dev_err(&master->dev,
  606. "failed to transfer one message from queue\n");
  607. return;
  608. }
  609. }
  610. static int spi_init_queue(struct spi_master *master)
  611. {
  612. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  613. INIT_LIST_HEAD(&master->queue);
  614. spin_lock_init(&master->queue_lock);
  615. master->running = false;
  616. master->busy = false;
  617. init_kthread_worker(&master->kworker);
  618. master->kworker_task = kthread_run(kthread_worker_fn,
  619. &master->kworker, "%s",
  620. dev_name(&master->dev));
  621. if (IS_ERR(master->kworker_task)) {
  622. dev_err(&master->dev, "failed to create message pump task\n");
  623. return -ENOMEM;
  624. }
  625. init_kthread_work(&master->pump_messages, spi_pump_messages);
  626. /*
  627. * Master config will indicate if this controller should run the
  628. * message pump with high (realtime) priority to reduce the transfer
  629. * latency on the bus by minimising the delay between a transfer
  630. * request and the scheduling of the message pump thread. Without this
  631. * setting the message pump thread will remain at default priority.
  632. */
  633. if (master->rt) {
  634. dev_info(&master->dev,
  635. "will run message pump with realtime priority\n");
  636. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  637. }
  638. return 0;
  639. }
  640. /**
  641. * spi_get_next_queued_message() - called by driver to check for queued
  642. * messages
  643. * @master: the master to check for queued messages
  644. *
  645. * If there are more messages in the queue, the next message is returned from
  646. * this call.
  647. */
  648. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  649. {
  650. struct spi_message *next;
  651. unsigned long flags;
  652. /* get a pointer to the next message, if any */
  653. spin_lock_irqsave(&master->queue_lock, flags);
  654. if (list_empty(&master->queue))
  655. next = NULL;
  656. else
  657. next = list_entry(master->queue.next,
  658. struct spi_message, queue);
  659. spin_unlock_irqrestore(&master->queue_lock, flags);
  660. return next;
  661. }
  662. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  663. /**
  664. * spi_finalize_current_message() - the current message is complete
  665. * @master: the master to return the message to
  666. *
  667. * Called by the driver to notify the core that the message in the front of the
  668. * queue is complete and can be removed from the queue.
  669. */
  670. void spi_finalize_current_message(struct spi_master *master)
  671. {
  672. struct spi_message *mesg;
  673. unsigned long flags;
  674. int ret;
  675. spin_lock_irqsave(&master->queue_lock, flags);
  676. mesg = master->cur_msg;
  677. master->cur_msg = NULL;
  678. queue_kthread_work(&master->kworker, &master->pump_messages);
  679. spin_unlock_irqrestore(&master->queue_lock, flags);
  680. if (master->cur_msg_prepared && master->unprepare_message) {
  681. ret = master->unprepare_message(master, mesg);
  682. if (ret) {
  683. dev_err(&master->dev,
  684. "failed to unprepare message: %d\n", ret);
  685. }
  686. }
  687. master->cur_msg_prepared = false;
  688. mesg->state = NULL;
  689. if (mesg->complete)
  690. mesg->complete(mesg->context);
  691. trace_spi_message_done(mesg);
  692. }
  693. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  694. static int spi_start_queue(struct spi_master *master)
  695. {
  696. unsigned long flags;
  697. spin_lock_irqsave(&master->queue_lock, flags);
  698. if (master->running || master->busy) {
  699. spin_unlock_irqrestore(&master->queue_lock, flags);
  700. return -EBUSY;
  701. }
  702. master->running = true;
  703. master->cur_msg = NULL;
  704. spin_unlock_irqrestore(&master->queue_lock, flags);
  705. queue_kthread_work(&master->kworker, &master->pump_messages);
  706. return 0;
  707. }
  708. static int spi_stop_queue(struct spi_master *master)
  709. {
  710. unsigned long flags;
  711. unsigned limit = 500;
  712. int ret = 0;
  713. spin_lock_irqsave(&master->queue_lock, flags);
  714. /*
  715. * This is a bit lame, but is optimized for the common execution path.
  716. * A wait_queue on the master->busy could be used, but then the common
  717. * execution path (pump_messages) would be required to call wake_up or
  718. * friends on every SPI message. Do this instead.
  719. */
  720. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  721. spin_unlock_irqrestore(&master->queue_lock, flags);
  722. msleep(10);
  723. spin_lock_irqsave(&master->queue_lock, flags);
  724. }
  725. if (!list_empty(&master->queue) || master->busy)
  726. ret = -EBUSY;
  727. else
  728. master->running = false;
  729. spin_unlock_irqrestore(&master->queue_lock, flags);
  730. if (ret) {
  731. dev_warn(&master->dev,
  732. "could not stop message queue\n");
  733. return ret;
  734. }
  735. return ret;
  736. }
  737. static int spi_destroy_queue(struct spi_master *master)
  738. {
  739. int ret;
  740. ret = spi_stop_queue(master);
  741. /*
  742. * flush_kthread_worker will block until all work is done.
  743. * If the reason that stop_queue timed out is that the work will never
  744. * finish, then it does no good to call flush/stop thread, so
  745. * return anyway.
  746. */
  747. if (ret) {
  748. dev_err(&master->dev, "problem destroying queue\n");
  749. return ret;
  750. }
  751. flush_kthread_worker(&master->kworker);
  752. kthread_stop(master->kworker_task);
  753. return 0;
  754. }
  755. /**
  756. * spi_queued_transfer - transfer function for queued transfers
  757. * @spi: spi device which is requesting transfer
  758. * @msg: spi message which is to handled is queued to driver queue
  759. */
  760. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  761. {
  762. struct spi_master *master = spi->master;
  763. unsigned long flags;
  764. spin_lock_irqsave(&master->queue_lock, flags);
  765. if (!master->running) {
  766. spin_unlock_irqrestore(&master->queue_lock, flags);
  767. return -ESHUTDOWN;
  768. }
  769. msg->actual_length = 0;
  770. msg->status = -EINPROGRESS;
  771. list_add_tail(&msg->queue, &master->queue);
  772. if (!master->busy)
  773. queue_kthread_work(&master->kworker, &master->pump_messages);
  774. spin_unlock_irqrestore(&master->queue_lock, flags);
  775. return 0;
  776. }
  777. static int spi_master_initialize_queue(struct spi_master *master)
  778. {
  779. int ret;
  780. master->queued = true;
  781. master->transfer = spi_queued_transfer;
  782. if (!master->transfer_one_message)
  783. master->transfer_one_message = spi_transfer_one_message;
  784. /* Initialize and start queue */
  785. ret = spi_init_queue(master);
  786. if (ret) {
  787. dev_err(&master->dev, "problem initializing queue\n");
  788. goto err_init_queue;
  789. }
  790. ret = spi_start_queue(master);
  791. if (ret) {
  792. dev_err(&master->dev, "problem starting queue\n");
  793. goto err_start_queue;
  794. }
  795. return 0;
  796. err_start_queue:
  797. err_init_queue:
  798. spi_destroy_queue(master);
  799. return ret;
  800. }
  801. /*-------------------------------------------------------------------------*/
  802. #if defined(CONFIG_OF)
  803. /**
  804. * of_register_spi_devices() - Register child devices onto the SPI bus
  805. * @master: Pointer to spi_master device
  806. *
  807. * Registers an spi_device for each child node of master node which has a 'reg'
  808. * property.
  809. */
  810. static void of_register_spi_devices(struct spi_master *master)
  811. {
  812. struct spi_device *spi;
  813. struct device_node *nc;
  814. int rc;
  815. u32 value;
  816. if (!master->dev.of_node)
  817. return;
  818. for_each_available_child_of_node(master->dev.of_node, nc) {
  819. /* Alloc an spi_device */
  820. spi = spi_alloc_device(master);
  821. if (!spi) {
  822. dev_err(&master->dev, "spi_device alloc error for %s\n",
  823. nc->full_name);
  824. spi_dev_put(spi);
  825. continue;
  826. }
  827. /* Select device driver */
  828. if (of_modalias_node(nc, spi->modalias,
  829. sizeof(spi->modalias)) < 0) {
  830. dev_err(&master->dev, "cannot find modalias for %s\n",
  831. nc->full_name);
  832. spi_dev_put(spi);
  833. continue;
  834. }
  835. /* Device address */
  836. rc = of_property_read_u32(nc, "reg", &value);
  837. if (rc) {
  838. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  839. nc->full_name, rc);
  840. spi_dev_put(spi);
  841. continue;
  842. }
  843. spi->chip_select = value;
  844. /* Mode (clock phase/polarity/etc.) */
  845. if (of_find_property(nc, "spi-cpha", NULL))
  846. spi->mode |= SPI_CPHA;
  847. if (of_find_property(nc, "spi-cpol", NULL))
  848. spi->mode |= SPI_CPOL;
  849. if (of_find_property(nc, "spi-cs-high", NULL))
  850. spi->mode |= SPI_CS_HIGH;
  851. if (of_find_property(nc, "spi-3wire", NULL))
  852. spi->mode |= SPI_3WIRE;
  853. /* Device DUAL/QUAD mode */
  854. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  855. switch (value) {
  856. case 1:
  857. break;
  858. case 2:
  859. spi->mode |= SPI_TX_DUAL;
  860. break;
  861. case 4:
  862. spi->mode |= SPI_TX_QUAD;
  863. break;
  864. default:
  865. dev_err(&master->dev,
  866. "spi-tx-bus-width %d not supported\n",
  867. value);
  868. spi_dev_put(spi);
  869. continue;
  870. }
  871. }
  872. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  873. switch (value) {
  874. case 1:
  875. break;
  876. case 2:
  877. spi->mode |= SPI_RX_DUAL;
  878. break;
  879. case 4:
  880. spi->mode |= SPI_RX_QUAD;
  881. break;
  882. default:
  883. dev_err(&master->dev,
  884. "spi-rx-bus-width %d not supported\n",
  885. value);
  886. spi_dev_put(spi);
  887. continue;
  888. }
  889. }
  890. /* Device speed */
  891. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  892. if (rc) {
  893. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  894. nc->full_name, rc);
  895. spi_dev_put(spi);
  896. continue;
  897. }
  898. spi->max_speed_hz = value;
  899. /* IRQ */
  900. spi->irq = irq_of_parse_and_map(nc, 0);
  901. /* Store a pointer to the node in the device structure */
  902. of_node_get(nc);
  903. spi->dev.of_node = nc;
  904. /* Register the new device */
  905. request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
  906. rc = spi_add_device(spi);
  907. if (rc) {
  908. dev_err(&master->dev, "spi_device register error %s\n",
  909. nc->full_name);
  910. spi_dev_put(spi);
  911. }
  912. }
  913. }
  914. #else
  915. static void of_register_spi_devices(struct spi_master *master) { }
  916. #endif
  917. #ifdef CONFIG_ACPI
  918. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  919. {
  920. struct spi_device *spi = data;
  921. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  922. struct acpi_resource_spi_serialbus *sb;
  923. sb = &ares->data.spi_serial_bus;
  924. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  925. spi->chip_select = sb->device_selection;
  926. spi->max_speed_hz = sb->connection_speed;
  927. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  928. spi->mode |= SPI_CPHA;
  929. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  930. spi->mode |= SPI_CPOL;
  931. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  932. spi->mode |= SPI_CS_HIGH;
  933. }
  934. } else if (spi->irq < 0) {
  935. struct resource r;
  936. if (acpi_dev_resource_interrupt(ares, 0, &r))
  937. spi->irq = r.start;
  938. }
  939. /* Always tell the ACPI core to skip this resource */
  940. return 1;
  941. }
  942. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  943. void *data, void **return_value)
  944. {
  945. struct spi_master *master = data;
  946. struct list_head resource_list;
  947. struct acpi_device *adev;
  948. struct spi_device *spi;
  949. int ret;
  950. if (acpi_bus_get_device(handle, &adev))
  951. return AE_OK;
  952. if (acpi_bus_get_status(adev) || !adev->status.present)
  953. return AE_OK;
  954. spi = spi_alloc_device(master);
  955. if (!spi) {
  956. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  957. dev_name(&adev->dev));
  958. return AE_NO_MEMORY;
  959. }
  960. ACPI_HANDLE_SET(&spi->dev, handle);
  961. spi->irq = -1;
  962. INIT_LIST_HEAD(&resource_list);
  963. ret = acpi_dev_get_resources(adev, &resource_list,
  964. acpi_spi_add_resource, spi);
  965. acpi_dev_free_resource_list(&resource_list);
  966. if (ret < 0 || !spi->max_speed_hz) {
  967. spi_dev_put(spi);
  968. return AE_OK;
  969. }
  970. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  971. if (spi_add_device(spi)) {
  972. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  973. dev_name(&adev->dev));
  974. spi_dev_put(spi);
  975. }
  976. return AE_OK;
  977. }
  978. static void acpi_register_spi_devices(struct spi_master *master)
  979. {
  980. acpi_status status;
  981. acpi_handle handle;
  982. handle = ACPI_HANDLE(master->dev.parent);
  983. if (!handle)
  984. return;
  985. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  986. acpi_spi_add_device, NULL,
  987. master, NULL);
  988. if (ACPI_FAILURE(status))
  989. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  990. }
  991. #else
  992. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  993. #endif /* CONFIG_ACPI */
  994. static void spi_master_release(struct device *dev)
  995. {
  996. struct spi_master *master;
  997. master = container_of(dev, struct spi_master, dev);
  998. kfree(master);
  999. }
  1000. static struct class spi_master_class = {
  1001. .name = "spi_master",
  1002. .owner = THIS_MODULE,
  1003. .dev_release = spi_master_release,
  1004. };
  1005. /**
  1006. * spi_alloc_master - allocate SPI master controller
  1007. * @dev: the controller, possibly using the platform_bus
  1008. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1009. * memory is in the driver_data field of the returned device,
  1010. * accessible with spi_master_get_devdata().
  1011. * Context: can sleep
  1012. *
  1013. * This call is used only by SPI master controller drivers, which are the
  1014. * only ones directly touching chip registers. It's how they allocate
  1015. * an spi_master structure, prior to calling spi_register_master().
  1016. *
  1017. * This must be called from context that can sleep. It returns the SPI
  1018. * master structure on success, else NULL.
  1019. *
  1020. * The caller is responsible for assigning the bus number and initializing
  1021. * the master's methods before calling spi_register_master(); and (after errors
  1022. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  1023. * leak.
  1024. */
  1025. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1026. {
  1027. struct spi_master *master;
  1028. if (!dev)
  1029. return NULL;
  1030. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1031. if (!master)
  1032. return NULL;
  1033. device_initialize(&master->dev);
  1034. master->bus_num = -1;
  1035. master->num_chipselect = 1;
  1036. master->dev.class = &spi_master_class;
  1037. master->dev.parent = get_device(dev);
  1038. spi_master_set_devdata(master, &master[1]);
  1039. return master;
  1040. }
  1041. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1042. #ifdef CONFIG_OF
  1043. static int of_spi_register_master(struct spi_master *master)
  1044. {
  1045. int nb, i, *cs;
  1046. struct device_node *np = master->dev.of_node;
  1047. if (!np)
  1048. return 0;
  1049. nb = of_gpio_named_count(np, "cs-gpios");
  1050. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1051. /* Return error only for an incorrectly formed cs-gpios property */
  1052. if (nb == 0 || nb == -ENOENT)
  1053. return 0;
  1054. else if (nb < 0)
  1055. return nb;
  1056. cs = devm_kzalloc(&master->dev,
  1057. sizeof(int) * master->num_chipselect,
  1058. GFP_KERNEL);
  1059. master->cs_gpios = cs;
  1060. if (!master->cs_gpios)
  1061. return -ENOMEM;
  1062. for (i = 0; i < master->num_chipselect; i++)
  1063. cs[i] = -ENOENT;
  1064. for (i = 0; i < nb; i++)
  1065. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1066. return 0;
  1067. }
  1068. #else
  1069. static int of_spi_register_master(struct spi_master *master)
  1070. {
  1071. return 0;
  1072. }
  1073. #endif
  1074. /**
  1075. * spi_register_master - register SPI master controller
  1076. * @master: initialized master, originally from spi_alloc_master()
  1077. * Context: can sleep
  1078. *
  1079. * SPI master controllers connect to their drivers using some non-SPI bus,
  1080. * such as the platform bus. The final stage of probe() in that code
  1081. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1082. *
  1083. * SPI controllers use board specific (often SOC specific) bus numbers,
  1084. * and board-specific addressing for SPI devices combines those numbers
  1085. * with chip select numbers. Since SPI does not directly support dynamic
  1086. * device identification, boards need configuration tables telling which
  1087. * chip is at which address.
  1088. *
  1089. * This must be called from context that can sleep. It returns zero on
  1090. * success, else a negative error code (dropping the master's refcount).
  1091. * After a successful return, the caller is responsible for calling
  1092. * spi_unregister_master().
  1093. */
  1094. int spi_register_master(struct spi_master *master)
  1095. {
  1096. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1097. struct device *dev = master->dev.parent;
  1098. struct boardinfo *bi;
  1099. int status = -ENODEV;
  1100. int dynamic = 0;
  1101. if (!dev)
  1102. return -ENODEV;
  1103. status = of_spi_register_master(master);
  1104. if (status)
  1105. return status;
  1106. /* even if it's just one always-selected device, there must
  1107. * be at least one chipselect
  1108. */
  1109. if (master->num_chipselect == 0)
  1110. return -EINVAL;
  1111. if ((master->bus_num < 0) && master->dev.of_node)
  1112. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1113. /* convention: dynamically assigned bus IDs count down from the max */
  1114. if (master->bus_num < 0) {
  1115. /* FIXME switch to an IDR based scheme, something like
  1116. * I2C now uses, so we can't run out of "dynamic" IDs
  1117. */
  1118. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1119. dynamic = 1;
  1120. }
  1121. spin_lock_init(&master->bus_lock_spinlock);
  1122. mutex_init(&master->bus_lock_mutex);
  1123. master->bus_lock_flag = 0;
  1124. init_completion(&master->xfer_completion);
  1125. /* register the device, then userspace will see it.
  1126. * registration fails if the bus ID is in use.
  1127. */
  1128. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1129. status = device_add(&master->dev);
  1130. if (status < 0)
  1131. goto done;
  1132. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1133. dynamic ? " (dynamic)" : "");
  1134. /* If we're using a queued driver, start the queue */
  1135. if (master->transfer)
  1136. dev_info(dev, "master is unqueued, this is deprecated\n");
  1137. else {
  1138. status = spi_master_initialize_queue(master);
  1139. if (status) {
  1140. device_del(&master->dev);
  1141. goto done;
  1142. }
  1143. }
  1144. mutex_lock(&board_lock);
  1145. list_add_tail(&master->list, &spi_master_list);
  1146. list_for_each_entry(bi, &board_list, list)
  1147. spi_match_master_to_boardinfo(master, &bi->board_info);
  1148. mutex_unlock(&board_lock);
  1149. /* Register devices from the device tree and ACPI */
  1150. of_register_spi_devices(master);
  1151. acpi_register_spi_devices(master);
  1152. done:
  1153. return status;
  1154. }
  1155. EXPORT_SYMBOL_GPL(spi_register_master);
  1156. static void devm_spi_unregister(struct device *dev, void *res)
  1157. {
  1158. spi_unregister_master(*(struct spi_master **)res);
  1159. }
  1160. /**
  1161. * dev_spi_register_master - register managed SPI master controller
  1162. * @dev: device managing SPI master
  1163. * @master: initialized master, originally from spi_alloc_master()
  1164. * Context: can sleep
  1165. *
  1166. * Register a SPI device as with spi_register_master() which will
  1167. * automatically be unregister
  1168. */
  1169. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1170. {
  1171. struct spi_master **ptr;
  1172. int ret;
  1173. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1174. if (!ptr)
  1175. return -ENOMEM;
  1176. ret = spi_register_master(master);
  1177. if (!ret) {
  1178. *ptr = master;
  1179. devres_add(dev, ptr);
  1180. } else {
  1181. devres_free(ptr);
  1182. }
  1183. return ret;
  1184. }
  1185. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1186. static int __unregister(struct device *dev, void *null)
  1187. {
  1188. spi_unregister_device(to_spi_device(dev));
  1189. return 0;
  1190. }
  1191. /**
  1192. * spi_unregister_master - unregister SPI master controller
  1193. * @master: the master being unregistered
  1194. * Context: can sleep
  1195. *
  1196. * This call is used only by SPI master controller drivers, which are the
  1197. * only ones directly touching chip registers.
  1198. *
  1199. * This must be called from context that can sleep.
  1200. */
  1201. void spi_unregister_master(struct spi_master *master)
  1202. {
  1203. int dummy;
  1204. if (master->queued) {
  1205. if (spi_destroy_queue(master))
  1206. dev_err(&master->dev, "queue remove failed\n");
  1207. }
  1208. mutex_lock(&board_lock);
  1209. list_del(&master->list);
  1210. mutex_unlock(&board_lock);
  1211. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1212. device_unregister(&master->dev);
  1213. }
  1214. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1215. int spi_master_suspend(struct spi_master *master)
  1216. {
  1217. int ret;
  1218. /* Basically no-ops for non-queued masters */
  1219. if (!master->queued)
  1220. return 0;
  1221. ret = spi_stop_queue(master);
  1222. if (ret)
  1223. dev_err(&master->dev, "queue stop failed\n");
  1224. return ret;
  1225. }
  1226. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1227. int spi_master_resume(struct spi_master *master)
  1228. {
  1229. int ret;
  1230. if (!master->queued)
  1231. return 0;
  1232. ret = spi_start_queue(master);
  1233. if (ret)
  1234. dev_err(&master->dev, "queue restart failed\n");
  1235. return ret;
  1236. }
  1237. EXPORT_SYMBOL_GPL(spi_master_resume);
  1238. static int __spi_master_match(struct device *dev, const void *data)
  1239. {
  1240. struct spi_master *m;
  1241. const u16 *bus_num = data;
  1242. m = container_of(dev, struct spi_master, dev);
  1243. return m->bus_num == *bus_num;
  1244. }
  1245. /**
  1246. * spi_busnum_to_master - look up master associated with bus_num
  1247. * @bus_num: the master's bus number
  1248. * Context: can sleep
  1249. *
  1250. * This call may be used with devices that are registered after
  1251. * arch init time. It returns a refcounted pointer to the relevant
  1252. * spi_master (which the caller must release), or NULL if there is
  1253. * no such master registered.
  1254. */
  1255. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1256. {
  1257. struct device *dev;
  1258. struct spi_master *master = NULL;
  1259. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1260. __spi_master_match);
  1261. if (dev)
  1262. master = container_of(dev, struct spi_master, dev);
  1263. /* reference got in class_find_device */
  1264. return master;
  1265. }
  1266. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1267. /*-------------------------------------------------------------------------*/
  1268. /* Core methods for SPI master protocol drivers. Some of the
  1269. * other core methods are currently defined as inline functions.
  1270. */
  1271. /**
  1272. * spi_setup - setup SPI mode and clock rate
  1273. * @spi: the device whose settings are being modified
  1274. * Context: can sleep, and no requests are queued to the device
  1275. *
  1276. * SPI protocol drivers may need to update the transfer mode if the
  1277. * device doesn't work with its default. They may likewise need
  1278. * to update clock rates or word sizes from initial values. This function
  1279. * changes those settings, and must be called from a context that can sleep.
  1280. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1281. * effect the next time the device is selected and data is transferred to
  1282. * or from it. When this function returns, the spi device is deselected.
  1283. *
  1284. * Note that this call will fail if the protocol driver specifies an option
  1285. * that the underlying controller or its driver does not support. For
  1286. * example, not all hardware supports wire transfers using nine bit words,
  1287. * LSB-first wire encoding, or active-high chipselects.
  1288. */
  1289. int spi_setup(struct spi_device *spi)
  1290. {
  1291. unsigned bad_bits;
  1292. int status = 0;
  1293. /* check mode to prevent that DUAL and QUAD set at the same time
  1294. */
  1295. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  1296. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  1297. dev_err(&spi->dev,
  1298. "setup: can not select dual and quad at the same time\n");
  1299. return -EINVAL;
  1300. }
  1301. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  1302. */
  1303. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  1304. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  1305. return -EINVAL;
  1306. /* help drivers fail *cleanly* when they need options
  1307. * that aren't supported with their current master
  1308. */
  1309. bad_bits = spi->mode & ~spi->master->mode_bits;
  1310. if (bad_bits) {
  1311. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1312. bad_bits);
  1313. return -EINVAL;
  1314. }
  1315. if (!spi->bits_per_word)
  1316. spi->bits_per_word = 8;
  1317. if (spi->master->setup)
  1318. status = spi->master->setup(spi);
  1319. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  1320. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1321. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1322. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1323. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1324. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1325. spi->bits_per_word, spi->max_speed_hz,
  1326. status);
  1327. return status;
  1328. }
  1329. EXPORT_SYMBOL_GPL(spi_setup);
  1330. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1331. {
  1332. struct spi_master *master = spi->master;
  1333. struct spi_transfer *xfer;
  1334. message->spi = spi;
  1335. trace_spi_message_submit(message);
  1336. if (list_empty(&message->transfers))
  1337. return -EINVAL;
  1338. if (!message->complete)
  1339. return -EINVAL;
  1340. /* Half-duplex links include original MicroWire, and ones with
  1341. * only one data pin like SPI_3WIRE (switches direction) or where
  1342. * either MOSI or MISO is missing. They can also be caused by
  1343. * software limitations.
  1344. */
  1345. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1346. || (spi->mode & SPI_3WIRE)) {
  1347. unsigned flags = master->flags;
  1348. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1349. if (xfer->rx_buf && xfer->tx_buf)
  1350. return -EINVAL;
  1351. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1352. return -EINVAL;
  1353. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1354. return -EINVAL;
  1355. }
  1356. }
  1357. /**
  1358. * Set transfer bits_per_word and max speed as spi device default if
  1359. * it is not set for this transfer.
  1360. * Set transfer tx_nbits and rx_nbits as single transfer default
  1361. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  1362. */
  1363. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1364. message->frame_length += xfer->len;
  1365. if (!xfer->bits_per_word)
  1366. xfer->bits_per_word = spi->bits_per_word;
  1367. if (!xfer->speed_hz) {
  1368. xfer->speed_hz = spi->max_speed_hz;
  1369. if (master->max_speed_hz &&
  1370. xfer->speed_hz > master->max_speed_hz)
  1371. xfer->speed_hz = master->max_speed_hz;
  1372. }
  1373. if (master->bits_per_word_mask) {
  1374. /* Only 32 bits fit in the mask */
  1375. if (xfer->bits_per_word > 32)
  1376. return -EINVAL;
  1377. if (!(master->bits_per_word_mask &
  1378. BIT(xfer->bits_per_word - 1)))
  1379. return -EINVAL;
  1380. }
  1381. if (xfer->speed_hz && master->min_speed_hz &&
  1382. xfer->speed_hz < master->min_speed_hz)
  1383. return -EINVAL;
  1384. if (xfer->speed_hz && master->max_speed_hz &&
  1385. xfer->speed_hz > master->max_speed_hz)
  1386. return -EINVAL;
  1387. if (xfer->tx_buf && !xfer->tx_nbits)
  1388. xfer->tx_nbits = SPI_NBITS_SINGLE;
  1389. if (xfer->rx_buf && !xfer->rx_nbits)
  1390. xfer->rx_nbits = SPI_NBITS_SINGLE;
  1391. /* check transfer tx/rx_nbits:
  1392. * 1. keep the value is not out of single, dual and quad
  1393. * 2. keep tx/rx_nbits is contained by mode in spi_device
  1394. * 3. if SPI_3WIRE, tx/rx_nbits should be in single
  1395. */
  1396. if (xfer->tx_buf) {
  1397. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  1398. xfer->tx_nbits != SPI_NBITS_DUAL &&
  1399. xfer->tx_nbits != SPI_NBITS_QUAD)
  1400. return -EINVAL;
  1401. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  1402. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  1403. return -EINVAL;
  1404. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  1405. !(spi->mode & SPI_TX_QUAD))
  1406. return -EINVAL;
  1407. if ((spi->mode & SPI_3WIRE) &&
  1408. (xfer->tx_nbits != SPI_NBITS_SINGLE))
  1409. return -EINVAL;
  1410. }
  1411. /* check transfer rx_nbits */
  1412. if (xfer->rx_buf) {
  1413. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  1414. xfer->rx_nbits != SPI_NBITS_DUAL &&
  1415. xfer->rx_nbits != SPI_NBITS_QUAD)
  1416. return -EINVAL;
  1417. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  1418. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  1419. return -EINVAL;
  1420. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  1421. !(spi->mode & SPI_RX_QUAD))
  1422. return -EINVAL;
  1423. if ((spi->mode & SPI_3WIRE) &&
  1424. (xfer->rx_nbits != SPI_NBITS_SINGLE))
  1425. return -EINVAL;
  1426. }
  1427. }
  1428. message->status = -EINPROGRESS;
  1429. return master->transfer(spi, message);
  1430. }
  1431. /**
  1432. * spi_async - asynchronous SPI transfer
  1433. * @spi: device with which data will be exchanged
  1434. * @message: describes the data transfers, including completion callback
  1435. * Context: any (irqs may be blocked, etc)
  1436. *
  1437. * This call may be used in_irq and other contexts which can't sleep,
  1438. * as well as from task contexts which can sleep.
  1439. *
  1440. * The completion callback is invoked in a context which can't sleep.
  1441. * Before that invocation, the value of message->status is undefined.
  1442. * When the callback is issued, message->status holds either zero (to
  1443. * indicate complete success) or a negative error code. After that
  1444. * callback returns, the driver which issued the transfer request may
  1445. * deallocate the associated memory; it's no longer in use by any SPI
  1446. * core or controller driver code.
  1447. *
  1448. * Note that although all messages to a spi_device are handled in
  1449. * FIFO order, messages may go to different devices in other orders.
  1450. * Some device might be higher priority, or have various "hard" access
  1451. * time requirements, for example.
  1452. *
  1453. * On detection of any fault during the transfer, processing of
  1454. * the entire message is aborted, and the device is deselected.
  1455. * Until returning from the associated message completion callback,
  1456. * no other spi_message queued to that device will be processed.
  1457. * (This rule applies equally to all the synchronous transfer calls,
  1458. * which are wrappers around this core asynchronous primitive.)
  1459. */
  1460. int spi_async(struct spi_device *spi, struct spi_message *message)
  1461. {
  1462. struct spi_master *master = spi->master;
  1463. int ret;
  1464. unsigned long flags;
  1465. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1466. if (master->bus_lock_flag)
  1467. ret = -EBUSY;
  1468. else
  1469. ret = __spi_async(spi, message);
  1470. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1471. return ret;
  1472. }
  1473. EXPORT_SYMBOL_GPL(spi_async);
  1474. /**
  1475. * spi_async_locked - version of spi_async with exclusive bus usage
  1476. * @spi: device with which data will be exchanged
  1477. * @message: describes the data transfers, including completion callback
  1478. * Context: any (irqs may be blocked, etc)
  1479. *
  1480. * This call may be used in_irq and other contexts which can't sleep,
  1481. * as well as from task contexts which can sleep.
  1482. *
  1483. * The completion callback is invoked in a context which can't sleep.
  1484. * Before that invocation, the value of message->status is undefined.
  1485. * When the callback is issued, message->status holds either zero (to
  1486. * indicate complete success) or a negative error code. After that
  1487. * callback returns, the driver which issued the transfer request may
  1488. * deallocate the associated memory; it's no longer in use by any SPI
  1489. * core or controller driver code.
  1490. *
  1491. * Note that although all messages to a spi_device are handled in
  1492. * FIFO order, messages may go to different devices in other orders.
  1493. * Some device might be higher priority, or have various "hard" access
  1494. * time requirements, for example.
  1495. *
  1496. * On detection of any fault during the transfer, processing of
  1497. * the entire message is aborted, and the device is deselected.
  1498. * Until returning from the associated message completion callback,
  1499. * no other spi_message queued to that device will be processed.
  1500. * (This rule applies equally to all the synchronous transfer calls,
  1501. * which are wrappers around this core asynchronous primitive.)
  1502. */
  1503. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1504. {
  1505. struct spi_master *master = spi->master;
  1506. int ret;
  1507. unsigned long flags;
  1508. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1509. ret = __spi_async(spi, message);
  1510. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1511. return ret;
  1512. }
  1513. EXPORT_SYMBOL_GPL(spi_async_locked);
  1514. /*-------------------------------------------------------------------------*/
  1515. /* Utility methods for SPI master protocol drivers, layered on
  1516. * top of the core. Some other utility methods are defined as
  1517. * inline functions.
  1518. */
  1519. static void spi_complete(void *arg)
  1520. {
  1521. complete(arg);
  1522. }
  1523. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1524. int bus_locked)
  1525. {
  1526. DECLARE_COMPLETION_ONSTACK(done);
  1527. int status;
  1528. struct spi_master *master = spi->master;
  1529. message->complete = spi_complete;
  1530. message->context = &done;
  1531. if (!bus_locked)
  1532. mutex_lock(&master->bus_lock_mutex);
  1533. status = spi_async_locked(spi, message);
  1534. if (!bus_locked)
  1535. mutex_unlock(&master->bus_lock_mutex);
  1536. if (status == 0) {
  1537. wait_for_completion(&done);
  1538. status = message->status;
  1539. }
  1540. message->context = NULL;
  1541. return status;
  1542. }
  1543. /**
  1544. * spi_sync - blocking/synchronous SPI data transfers
  1545. * @spi: device with which data will be exchanged
  1546. * @message: describes the data transfers
  1547. * Context: can sleep
  1548. *
  1549. * This call may only be used from a context that may sleep. The sleep
  1550. * is non-interruptible, and has no timeout. Low-overhead controller
  1551. * drivers may DMA directly into and out of the message buffers.
  1552. *
  1553. * Note that the SPI device's chip select is active during the message,
  1554. * and then is normally disabled between messages. Drivers for some
  1555. * frequently-used devices may want to minimize costs of selecting a chip,
  1556. * by leaving it selected in anticipation that the next message will go
  1557. * to the same chip. (That may increase power usage.)
  1558. *
  1559. * Also, the caller is guaranteeing that the memory associated with the
  1560. * message will not be freed before this call returns.
  1561. *
  1562. * It returns zero on success, else a negative error code.
  1563. */
  1564. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1565. {
  1566. return __spi_sync(spi, message, 0);
  1567. }
  1568. EXPORT_SYMBOL_GPL(spi_sync);
  1569. /**
  1570. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1571. * @spi: device with which data will be exchanged
  1572. * @message: describes the data transfers
  1573. * Context: can sleep
  1574. *
  1575. * This call may only be used from a context that may sleep. The sleep
  1576. * is non-interruptible, and has no timeout. Low-overhead controller
  1577. * drivers may DMA directly into and out of the message buffers.
  1578. *
  1579. * This call should be used by drivers that require exclusive access to the
  1580. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1581. * be released by a spi_bus_unlock call when the exclusive access is over.
  1582. *
  1583. * It returns zero on success, else a negative error code.
  1584. */
  1585. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1586. {
  1587. return __spi_sync(spi, message, 1);
  1588. }
  1589. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1590. /**
  1591. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1592. * @master: SPI bus master that should be locked for exclusive bus access
  1593. * Context: can sleep
  1594. *
  1595. * This call may only be used from a context that may sleep. The sleep
  1596. * is non-interruptible, and has no timeout.
  1597. *
  1598. * This call should be used by drivers that require exclusive access to the
  1599. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1600. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1601. * and spi_async_locked calls when the SPI bus lock is held.
  1602. *
  1603. * It returns zero on success, else a negative error code.
  1604. */
  1605. int spi_bus_lock(struct spi_master *master)
  1606. {
  1607. unsigned long flags;
  1608. mutex_lock(&master->bus_lock_mutex);
  1609. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1610. master->bus_lock_flag = 1;
  1611. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1612. /* mutex remains locked until spi_bus_unlock is called */
  1613. return 0;
  1614. }
  1615. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1616. /**
  1617. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1618. * @master: SPI bus master that was locked for exclusive bus access
  1619. * Context: can sleep
  1620. *
  1621. * This call may only be used from a context that may sleep. The sleep
  1622. * is non-interruptible, and has no timeout.
  1623. *
  1624. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1625. * call.
  1626. *
  1627. * It returns zero on success, else a negative error code.
  1628. */
  1629. int spi_bus_unlock(struct spi_master *master)
  1630. {
  1631. master->bus_lock_flag = 0;
  1632. mutex_unlock(&master->bus_lock_mutex);
  1633. return 0;
  1634. }
  1635. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1636. /* portable code must never pass more than 32 bytes */
  1637. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  1638. static u8 *buf;
  1639. /**
  1640. * spi_write_then_read - SPI synchronous write followed by read
  1641. * @spi: device with which data will be exchanged
  1642. * @txbuf: data to be written (need not be dma-safe)
  1643. * @n_tx: size of txbuf, in bytes
  1644. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1645. * @n_rx: size of rxbuf, in bytes
  1646. * Context: can sleep
  1647. *
  1648. * This performs a half duplex MicroWire style transaction with the
  1649. * device, sending txbuf and then reading rxbuf. The return value
  1650. * is zero for success, else a negative errno status code.
  1651. * This call may only be used from a context that may sleep.
  1652. *
  1653. * Parameters to this routine are always copied using a small buffer;
  1654. * portable code should never use this for more than 32 bytes.
  1655. * Performance-sensitive or bulk transfer code should instead use
  1656. * spi_{async,sync}() calls with dma-safe buffers.
  1657. */
  1658. int spi_write_then_read(struct spi_device *spi,
  1659. const void *txbuf, unsigned n_tx,
  1660. void *rxbuf, unsigned n_rx)
  1661. {
  1662. static DEFINE_MUTEX(lock);
  1663. int status;
  1664. struct spi_message message;
  1665. struct spi_transfer x[2];
  1666. u8 *local_buf;
  1667. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1668. * copying here, (as a pure convenience thing), but we can
  1669. * keep heap costs out of the hot path unless someone else is
  1670. * using the pre-allocated buffer or the transfer is too large.
  1671. */
  1672. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1673. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1674. GFP_KERNEL | GFP_DMA);
  1675. if (!local_buf)
  1676. return -ENOMEM;
  1677. } else {
  1678. local_buf = buf;
  1679. }
  1680. spi_message_init(&message);
  1681. memset(x, 0, sizeof(x));
  1682. if (n_tx) {
  1683. x[0].len = n_tx;
  1684. spi_message_add_tail(&x[0], &message);
  1685. }
  1686. if (n_rx) {
  1687. x[1].len = n_rx;
  1688. spi_message_add_tail(&x[1], &message);
  1689. }
  1690. memcpy(local_buf, txbuf, n_tx);
  1691. x[0].tx_buf = local_buf;
  1692. x[1].rx_buf = local_buf + n_tx;
  1693. /* do the i/o */
  1694. status = spi_sync(spi, &message);
  1695. if (status == 0)
  1696. memcpy(rxbuf, x[1].rx_buf, n_rx);
  1697. if (x[0].tx_buf == buf)
  1698. mutex_unlock(&lock);
  1699. else
  1700. kfree(local_buf);
  1701. return status;
  1702. }
  1703. EXPORT_SYMBOL_GPL(spi_write_then_read);
  1704. /*-------------------------------------------------------------------------*/
  1705. static int __init spi_init(void)
  1706. {
  1707. int status;
  1708. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  1709. if (!buf) {
  1710. status = -ENOMEM;
  1711. goto err0;
  1712. }
  1713. status = bus_register(&spi_bus_type);
  1714. if (status < 0)
  1715. goto err1;
  1716. status = class_register(&spi_master_class);
  1717. if (status < 0)
  1718. goto err2;
  1719. return 0;
  1720. err2:
  1721. bus_unregister(&spi_bus_type);
  1722. err1:
  1723. kfree(buf);
  1724. buf = NULL;
  1725. err0:
  1726. return status;
  1727. }
  1728. /* board_info is normally registered in arch_initcall(),
  1729. * but even essential drivers wait till later
  1730. *
  1731. * REVISIT only boardinfo really needs static linking. the rest (device and
  1732. * driver registration) _could_ be dynamically linked (modular) ... costs
  1733. * include needing to have boardinfo data structures be much more public.
  1734. */
  1735. postcore_initcall(spi_init);