spi.c 55 KB

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