spi.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  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. int ms = 1;
  507. spi_set_cs(msg->spi, true);
  508. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  509. trace_spi_transfer_start(msg, xfer);
  510. reinit_completion(&master->xfer_completion);
  511. ret = master->transfer_one(master, msg->spi, xfer);
  512. if (ret < 0) {
  513. dev_err(&msg->spi->dev,
  514. "SPI transfer failed: %d\n", ret);
  515. goto out;
  516. }
  517. if (ret > 0) {
  518. ret = 0;
  519. ms = xfer->len * 8 * 1000 / xfer->speed_hz;
  520. ms += 10; /* some tolerance */
  521. ms = wait_for_completion_timeout(&master->xfer_completion,
  522. msecs_to_jiffies(ms));
  523. }
  524. if (ms == 0) {
  525. dev_err(&msg->spi->dev, "SPI transfer timed out\n");
  526. msg->status = -ETIMEDOUT;
  527. }
  528. trace_spi_transfer_stop(msg, xfer);
  529. if (msg->status != -EINPROGRESS)
  530. goto out;
  531. if (xfer->delay_usecs)
  532. udelay(xfer->delay_usecs);
  533. if (xfer->cs_change) {
  534. if (list_is_last(&xfer->transfer_list,
  535. &msg->transfers)) {
  536. keep_cs = true;
  537. } else {
  538. cur_cs = !cur_cs;
  539. spi_set_cs(msg->spi, cur_cs);
  540. }
  541. }
  542. msg->actual_length += xfer->len;
  543. }
  544. out:
  545. if (ret != 0 || !keep_cs)
  546. spi_set_cs(msg->spi, false);
  547. if (msg->status == -EINPROGRESS)
  548. msg->status = ret;
  549. spi_finalize_current_message(master);
  550. return ret;
  551. }
  552. /**
  553. * spi_finalize_current_transfer - report completion of a transfer
  554. *
  555. * Called by SPI drivers using the core transfer_one_message()
  556. * implementation to notify it that the current interrupt driven
  557. * transfer has finished and the next one may be scheduled.
  558. */
  559. void spi_finalize_current_transfer(struct spi_master *master)
  560. {
  561. complete(&master->xfer_completion);
  562. }
  563. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  564. /**
  565. * spi_pump_messages - kthread work function which processes spi message queue
  566. * @work: pointer to kthread work struct contained in the master struct
  567. *
  568. * This function checks if there is any spi message in the queue that
  569. * needs processing and if so call out to the driver to initialize hardware
  570. * and transfer each message.
  571. *
  572. */
  573. static void spi_pump_messages(struct kthread_work *work)
  574. {
  575. struct spi_master *master =
  576. container_of(work, struct spi_master, pump_messages);
  577. unsigned long flags;
  578. bool was_busy = false;
  579. int ret;
  580. /* Lock queue and check for queue work */
  581. spin_lock_irqsave(&master->queue_lock, flags);
  582. if (list_empty(&master->queue) || !master->running) {
  583. if (!master->busy) {
  584. spin_unlock_irqrestore(&master->queue_lock, flags);
  585. return;
  586. }
  587. master->busy = false;
  588. spin_unlock_irqrestore(&master->queue_lock, flags);
  589. if (master->unprepare_transfer_hardware &&
  590. master->unprepare_transfer_hardware(master))
  591. dev_err(&master->dev,
  592. "failed to unprepare transfer hardware\n");
  593. if (master->auto_runtime_pm) {
  594. pm_runtime_mark_last_busy(master->dev.parent);
  595. pm_runtime_put_autosuspend(master->dev.parent);
  596. }
  597. trace_spi_master_idle(master);
  598. return;
  599. }
  600. /* Make sure we are not already running a message */
  601. if (master->cur_msg) {
  602. spin_unlock_irqrestore(&master->queue_lock, flags);
  603. return;
  604. }
  605. /* Extract head of queue */
  606. master->cur_msg =
  607. list_first_entry(&master->queue, struct spi_message, queue);
  608. list_del_init(&master->cur_msg->queue);
  609. if (master->busy)
  610. was_busy = true;
  611. else
  612. master->busy = true;
  613. spin_unlock_irqrestore(&master->queue_lock, flags);
  614. if (!was_busy && master->auto_runtime_pm) {
  615. ret = pm_runtime_get_sync(master->dev.parent);
  616. if (ret < 0) {
  617. dev_err(&master->dev, "Failed to power device: %d\n",
  618. ret);
  619. return;
  620. }
  621. }
  622. if (!was_busy)
  623. trace_spi_master_busy(master);
  624. if (!was_busy && master->prepare_transfer_hardware) {
  625. ret = master->prepare_transfer_hardware(master);
  626. if (ret) {
  627. dev_err(&master->dev,
  628. "failed to prepare transfer hardware\n");
  629. if (master->auto_runtime_pm)
  630. pm_runtime_put(master->dev.parent);
  631. return;
  632. }
  633. }
  634. trace_spi_message_start(master->cur_msg);
  635. if (master->prepare_message) {
  636. ret = master->prepare_message(master, master->cur_msg);
  637. if (ret) {
  638. dev_err(&master->dev,
  639. "failed to prepare message: %d\n", ret);
  640. master->cur_msg->status = ret;
  641. spi_finalize_current_message(master);
  642. return;
  643. }
  644. master->cur_msg_prepared = true;
  645. }
  646. ret = master->transfer_one_message(master, master->cur_msg);
  647. if (ret) {
  648. dev_err(&master->dev,
  649. "failed to transfer one message from queue: %d\n", ret);
  650. master->cur_msg->status = ret;
  651. spi_finalize_current_message(master);
  652. return;
  653. }
  654. }
  655. static int spi_init_queue(struct spi_master *master)
  656. {
  657. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  658. INIT_LIST_HEAD(&master->queue);
  659. spin_lock_init(&master->queue_lock);
  660. master->running = false;
  661. master->busy = false;
  662. init_kthread_worker(&master->kworker);
  663. master->kworker_task = kthread_run(kthread_worker_fn,
  664. &master->kworker, "%s",
  665. dev_name(&master->dev));
  666. if (IS_ERR(master->kworker_task)) {
  667. dev_err(&master->dev, "failed to create message pump task\n");
  668. return -ENOMEM;
  669. }
  670. init_kthread_work(&master->pump_messages, spi_pump_messages);
  671. /*
  672. * Master config will indicate if this controller should run the
  673. * message pump with high (realtime) priority to reduce the transfer
  674. * latency on the bus by minimising the delay between a transfer
  675. * request and the scheduling of the message pump thread. Without this
  676. * setting the message pump thread will remain at default priority.
  677. */
  678. if (master->rt) {
  679. dev_info(&master->dev,
  680. "will run message pump with realtime priority\n");
  681. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  682. }
  683. return 0;
  684. }
  685. /**
  686. * spi_get_next_queued_message() - called by driver to check for queued
  687. * messages
  688. * @master: the master to check for queued messages
  689. *
  690. * If there are more messages in the queue, the next message is returned from
  691. * this call.
  692. */
  693. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  694. {
  695. struct spi_message *next;
  696. unsigned long flags;
  697. /* get a pointer to the next message, if any */
  698. spin_lock_irqsave(&master->queue_lock, flags);
  699. next = list_first_entry_or_null(&master->queue, struct spi_message,
  700. queue);
  701. spin_unlock_irqrestore(&master->queue_lock, flags);
  702. return next;
  703. }
  704. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  705. /**
  706. * spi_finalize_current_message() - the current message is complete
  707. * @master: the master to return the message to
  708. *
  709. * Called by the driver to notify the core that the message in the front of the
  710. * queue is complete and can be removed from the queue.
  711. */
  712. void spi_finalize_current_message(struct spi_master *master)
  713. {
  714. struct spi_message *mesg;
  715. unsigned long flags;
  716. int ret;
  717. spin_lock_irqsave(&master->queue_lock, flags);
  718. mesg = master->cur_msg;
  719. master->cur_msg = NULL;
  720. queue_kthread_work(&master->kworker, &master->pump_messages);
  721. spin_unlock_irqrestore(&master->queue_lock, flags);
  722. if (master->cur_msg_prepared && master->unprepare_message) {
  723. ret = master->unprepare_message(master, mesg);
  724. if (ret) {
  725. dev_err(&master->dev,
  726. "failed to unprepare message: %d\n", ret);
  727. }
  728. }
  729. master->cur_msg_prepared = false;
  730. mesg->state = NULL;
  731. if (mesg->complete)
  732. mesg->complete(mesg->context);
  733. trace_spi_message_done(mesg);
  734. }
  735. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  736. static int spi_start_queue(struct spi_master *master)
  737. {
  738. unsigned long flags;
  739. spin_lock_irqsave(&master->queue_lock, flags);
  740. if (master->running || master->busy) {
  741. spin_unlock_irqrestore(&master->queue_lock, flags);
  742. return -EBUSY;
  743. }
  744. master->running = true;
  745. master->cur_msg = NULL;
  746. spin_unlock_irqrestore(&master->queue_lock, flags);
  747. queue_kthread_work(&master->kworker, &master->pump_messages);
  748. return 0;
  749. }
  750. static int spi_stop_queue(struct spi_master *master)
  751. {
  752. unsigned long flags;
  753. unsigned limit = 500;
  754. int ret = 0;
  755. spin_lock_irqsave(&master->queue_lock, flags);
  756. /*
  757. * This is a bit lame, but is optimized for the common execution path.
  758. * A wait_queue on the master->busy could be used, but then the common
  759. * execution path (pump_messages) would be required to call wake_up or
  760. * friends on every SPI message. Do this instead.
  761. */
  762. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  763. spin_unlock_irqrestore(&master->queue_lock, flags);
  764. msleep(10);
  765. spin_lock_irqsave(&master->queue_lock, flags);
  766. }
  767. if (!list_empty(&master->queue) || master->busy)
  768. ret = -EBUSY;
  769. else
  770. master->running = false;
  771. spin_unlock_irqrestore(&master->queue_lock, flags);
  772. if (ret) {
  773. dev_warn(&master->dev,
  774. "could not stop message queue\n");
  775. return ret;
  776. }
  777. return ret;
  778. }
  779. static int spi_destroy_queue(struct spi_master *master)
  780. {
  781. int ret;
  782. ret = spi_stop_queue(master);
  783. /*
  784. * flush_kthread_worker will block until all work is done.
  785. * If the reason that stop_queue timed out is that the work will never
  786. * finish, then it does no good to call flush/stop thread, so
  787. * return anyway.
  788. */
  789. if (ret) {
  790. dev_err(&master->dev, "problem destroying queue\n");
  791. return ret;
  792. }
  793. flush_kthread_worker(&master->kworker);
  794. kthread_stop(master->kworker_task);
  795. return 0;
  796. }
  797. /**
  798. * spi_queued_transfer - transfer function for queued transfers
  799. * @spi: spi device which is requesting transfer
  800. * @msg: spi message which is to handled is queued to driver queue
  801. */
  802. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  803. {
  804. struct spi_master *master = spi->master;
  805. unsigned long flags;
  806. spin_lock_irqsave(&master->queue_lock, flags);
  807. if (!master->running) {
  808. spin_unlock_irqrestore(&master->queue_lock, flags);
  809. return -ESHUTDOWN;
  810. }
  811. msg->actual_length = 0;
  812. msg->status = -EINPROGRESS;
  813. list_add_tail(&msg->queue, &master->queue);
  814. if (!master->busy)
  815. queue_kthread_work(&master->kworker, &master->pump_messages);
  816. spin_unlock_irqrestore(&master->queue_lock, flags);
  817. return 0;
  818. }
  819. static int spi_master_initialize_queue(struct spi_master *master)
  820. {
  821. int ret;
  822. master->queued = true;
  823. master->transfer = spi_queued_transfer;
  824. if (!master->transfer_one_message)
  825. master->transfer_one_message = spi_transfer_one_message;
  826. /* Initialize and start queue */
  827. ret = spi_init_queue(master);
  828. if (ret) {
  829. dev_err(&master->dev, "problem initializing queue\n");
  830. goto err_init_queue;
  831. }
  832. ret = spi_start_queue(master);
  833. if (ret) {
  834. dev_err(&master->dev, "problem starting queue\n");
  835. goto err_start_queue;
  836. }
  837. return 0;
  838. err_start_queue:
  839. err_init_queue:
  840. spi_destroy_queue(master);
  841. return ret;
  842. }
  843. /*-------------------------------------------------------------------------*/
  844. #if defined(CONFIG_OF)
  845. /**
  846. * of_register_spi_devices() - Register child devices onto the SPI bus
  847. * @master: Pointer to spi_master device
  848. *
  849. * Registers an spi_device for each child node of master node which has a 'reg'
  850. * property.
  851. */
  852. static void of_register_spi_devices(struct spi_master *master)
  853. {
  854. struct spi_device *spi;
  855. struct device_node *nc;
  856. int rc;
  857. u32 value;
  858. if (!master->dev.of_node)
  859. return;
  860. for_each_available_child_of_node(master->dev.of_node, nc) {
  861. /* Alloc an spi_device */
  862. spi = spi_alloc_device(master);
  863. if (!spi) {
  864. dev_err(&master->dev, "spi_device alloc error for %s\n",
  865. nc->full_name);
  866. spi_dev_put(spi);
  867. continue;
  868. }
  869. /* Select device driver */
  870. if (of_modalias_node(nc, spi->modalias,
  871. sizeof(spi->modalias)) < 0) {
  872. dev_err(&master->dev, "cannot find modalias for %s\n",
  873. nc->full_name);
  874. spi_dev_put(spi);
  875. continue;
  876. }
  877. /* Device address */
  878. rc = of_property_read_u32(nc, "reg", &value);
  879. if (rc) {
  880. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  881. nc->full_name, rc);
  882. spi_dev_put(spi);
  883. continue;
  884. }
  885. spi->chip_select = value;
  886. /* Mode (clock phase/polarity/etc.) */
  887. if (of_find_property(nc, "spi-cpha", NULL))
  888. spi->mode |= SPI_CPHA;
  889. if (of_find_property(nc, "spi-cpol", NULL))
  890. spi->mode |= SPI_CPOL;
  891. if (of_find_property(nc, "spi-cs-high", NULL))
  892. spi->mode |= SPI_CS_HIGH;
  893. if (of_find_property(nc, "spi-3wire", NULL))
  894. spi->mode |= SPI_3WIRE;
  895. /* Device DUAL/QUAD mode */
  896. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  897. switch (value) {
  898. case 1:
  899. break;
  900. case 2:
  901. spi->mode |= SPI_TX_DUAL;
  902. break;
  903. case 4:
  904. spi->mode |= SPI_TX_QUAD;
  905. break;
  906. default:
  907. dev_err(&master->dev,
  908. "spi-tx-bus-width %d not supported\n",
  909. value);
  910. spi_dev_put(spi);
  911. continue;
  912. }
  913. }
  914. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  915. switch (value) {
  916. case 1:
  917. break;
  918. case 2:
  919. spi->mode |= SPI_RX_DUAL;
  920. break;
  921. case 4:
  922. spi->mode |= SPI_RX_QUAD;
  923. break;
  924. default:
  925. dev_err(&master->dev,
  926. "spi-rx-bus-width %d not supported\n",
  927. value);
  928. spi_dev_put(spi);
  929. continue;
  930. }
  931. }
  932. /* Device speed */
  933. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  934. if (rc) {
  935. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  936. nc->full_name, rc);
  937. spi_dev_put(spi);
  938. continue;
  939. }
  940. spi->max_speed_hz = value;
  941. /* IRQ */
  942. spi->irq = irq_of_parse_and_map(nc, 0);
  943. /* Store a pointer to the node in the device structure */
  944. of_node_get(nc);
  945. spi->dev.of_node = nc;
  946. /* Register the new device */
  947. request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
  948. rc = spi_add_device(spi);
  949. if (rc) {
  950. dev_err(&master->dev, "spi_device register error %s\n",
  951. nc->full_name);
  952. spi_dev_put(spi);
  953. }
  954. }
  955. }
  956. #else
  957. static void of_register_spi_devices(struct spi_master *master) { }
  958. #endif
  959. #ifdef CONFIG_ACPI
  960. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  961. {
  962. struct spi_device *spi = data;
  963. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  964. struct acpi_resource_spi_serialbus *sb;
  965. sb = &ares->data.spi_serial_bus;
  966. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  967. spi->chip_select = sb->device_selection;
  968. spi->max_speed_hz = sb->connection_speed;
  969. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  970. spi->mode |= SPI_CPHA;
  971. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  972. spi->mode |= SPI_CPOL;
  973. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  974. spi->mode |= SPI_CS_HIGH;
  975. }
  976. } else if (spi->irq < 0) {
  977. struct resource r;
  978. if (acpi_dev_resource_interrupt(ares, 0, &r))
  979. spi->irq = r.start;
  980. }
  981. /* Always tell the ACPI core to skip this resource */
  982. return 1;
  983. }
  984. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  985. void *data, void **return_value)
  986. {
  987. struct spi_master *master = data;
  988. struct list_head resource_list;
  989. struct acpi_device *adev;
  990. struct spi_device *spi;
  991. int ret;
  992. if (acpi_bus_get_device(handle, &adev))
  993. return AE_OK;
  994. if (acpi_bus_get_status(adev) || !adev->status.present)
  995. return AE_OK;
  996. spi = spi_alloc_device(master);
  997. if (!spi) {
  998. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  999. dev_name(&adev->dev));
  1000. return AE_NO_MEMORY;
  1001. }
  1002. ACPI_COMPANION_SET(&spi->dev, adev);
  1003. spi->irq = -1;
  1004. INIT_LIST_HEAD(&resource_list);
  1005. ret = acpi_dev_get_resources(adev, &resource_list,
  1006. acpi_spi_add_resource, spi);
  1007. acpi_dev_free_resource_list(&resource_list);
  1008. if (ret < 0 || !spi->max_speed_hz) {
  1009. spi_dev_put(spi);
  1010. return AE_OK;
  1011. }
  1012. adev->power.flags.ignore_parent = true;
  1013. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  1014. if (spi_add_device(spi)) {
  1015. adev->power.flags.ignore_parent = false;
  1016. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  1017. dev_name(&adev->dev));
  1018. spi_dev_put(spi);
  1019. }
  1020. return AE_OK;
  1021. }
  1022. static void acpi_register_spi_devices(struct spi_master *master)
  1023. {
  1024. acpi_status status;
  1025. acpi_handle handle;
  1026. handle = ACPI_HANDLE(master->dev.parent);
  1027. if (!handle)
  1028. return;
  1029. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1030. acpi_spi_add_device, NULL,
  1031. master, NULL);
  1032. if (ACPI_FAILURE(status))
  1033. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1034. }
  1035. #else
  1036. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1037. #endif /* CONFIG_ACPI */
  1038. static void spi_master_release(struct device *dev)
  1039. {
  1040. struct spi_master *master;
  1041. master = container_of(dev, struct spi_master, dev);
  1042. kfree(master);
  1043. }
  1044. static struct class spi_master_class = {
  1045. .name = "spi_master",
  1046. .owner = THIS_MODULE,
  1047. .dev_release = spi_master_release,
  1048. };
  1049. /**
  1050. * spi_alloc_master - allocate SPI master controller
  1051. * @dev: the controller, possibly using the platform_bus
  1052. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1053. * memory is in the driver_data field of the returned device,
  1054. * accessible with spi_master_get_devdata().
  1055. * Context: can sleep
  1056. *
  1057. * This call is used only by SPI master controller drivers, which are the
  1058. * only ones directly touching chip registers. It's how they allocate
  1059. * an spi_master structure, prior to calling spi_register_master().
  1060. *
  1061. * This must be called from context that can sleep. It returns the SPI
  1062. * master structure on success, else NULL.
  1063. *
  1064. * The caller is responsible for assigning the bus number and initializing
  1065. * the master's methods before calling spi_register_master(); and (after errors
  1066. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  1067. * leak.
  1068. */
  1069. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1070. {
  1071. struct spi_master *master;
  1072. if (!dev)
  1073. return NULL;
  1074. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1075. if (!master)
  1076. return NULL;
  1077. device_initialize(&master->dev);
  1078. master->bus_num = -1;
  1079. master->num_chipselect = 1;
  1080. master->dev.class = &spi_master_class;
  1081. master->dev.parent = get_device(dev);
  1082. spi_master_set_devdata(master, &master[1]);
  1083. return master;
  1084. }
  1085. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1086. #ifdef CONFIG_OF
  1087. static int of_spi_register_master(struct spi_master *master)
  1088. {
  1089. int nb, i, *cs;
  1090. struct device_node *np = master->dev.of_node;
  1091. if (!np)
  1092. return 0;
  1093. nb = of_gpio_named_count(np, "cs-gpios");
  1094. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1095. /* Return error only for an incorrectly formed cs-gpios property */
  1096. if (nb == 0 || nb == -ENOENT)
  1097. return 0;
  1098. else if (nb < 0)
  1099. return nb;
  1100. cs = devm_kzalloc(&master->dev,
  1101. sizeof(int) * master->num_chipselect,
  1102. GFP_KERNEL);
  1103. master->cs_gpios = cs;
  1104. if (!master->cs_gpios)
  1105. return -ENOMEM;
  1106. for (i = 0; i < master->num_chipselect; i++)
  1107. cs[i] = -ENOENT;
  1108. for (i = 0; i < nb; i++)
  1109. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1110. return 0;
  1111. }
  1112. #else
  1113. static int of_spi_register_master(struct spi_master *master)
  1114. {
  1115. return 0;
  1116. }
  1117. #endif
  1118. /**
  1119. * spi_register_master - register SPI master controller
  1120. * @master: initialized master, originally from spi_alloc_master()
  1121. * Context: can sleep
  1122. *
  1123. * SPI master controllers connect to their drivers using some non-SPI bus,
  1124. * such as the platform bus. The final stage of probe() in that code
  1125. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1126. *
  1127. * SPI controllers use board specific (often SOC specific) bus numbers,
  1128. * and board-specific addressing for SPI devices combines those numbers
  1129. * with chip select numbers. Since SPI does not directly support dynamic
  1130. * device identification, boards need configuration tables telling which
  1131. * chip is at which address.
  1132. *
  1133. * This must be called from context that can sleep. It returns zero on
  1134. * success, else a negative error code (dropping the master's refcount).
  1135. * After a successful return, the caller is responsible for calling
  1136. * spi_unregister_master().
  1137. */
  1138. int spi_register_master(struct spi_master *master)
  1139. {
  1140. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1141. struct device *dev = master->dev.parent;
  1142. struct boardinfo *bi;
  1143. int status = -ENODEV;
  1144. int dynamic = 0;
  1145. if (!dev)
  1146. return -ENODEV;
  1147. status = of_spi_register_master(master);
  1148. if (status)
  1149. return status;
  1150. /* even if it's just one always-selected device, there must
  1151. * be at least one chipselect
  1152. */
  1153. if (master->num_chipselect == 0)
  1154. return -EINVAL;
  1155. if ((master->bus_num < 0) && master->dev.of_node)
  1156. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1157. /* convention: dynamically assigned bus IDs count down from the max */
  1158. if (master->bus_num < 0) {
  1159. /* FIXME switch to an IDR based scheme, something like
  1160. * I2C now uses, so we can't run out of "dynamic" IDs
  1161. */
  1162. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1163. dynamic = 1;
  1164. }
  1165. spin_lock_init(&master->bus_lock_spinlock);
  1166. mutex_init(&master->bus_lock_mutex);
  1167. master->bus_lock_flag = 0;
  1168. init_completion(&master->xfer_completion);
  1169. /* register the device, then userspace will see it.
  1170. * registration fails if the bus ID is in use.
  1171. */
  1172. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1173. status = device_add(&master->dev);
  1174. if (status < 0)
  1175. goto done;
  1176. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1177. dynamic ? " (dynamic)" : "");
  1178. /* If we're using a queued driver, start the queue */
  1179. if (master->transfer)
  1180. dev_info(dev, "master is unqueued, this is deprecated\n");
  1181. else {
  1182. status = spi_master_initialize_queue(master);
  1183. if (status) {
  1184. device_del(&master->dev);
  1185. goto done;
  1186. }
  1187. }
  1188. mutex_lock(&board_lock);
  1189. list_add_tail(&master->list, &spi_master_list);
  1190. list_for_each_entry(bi, &board_list, list)
  1191. spi_match_master_to_boardinfo(master, &bi->board_info);
  1192. mutex_unlock(&board_lock);
  1193. /* Register devices from the device tree and ACPI */
  1194. of_register_spi_devices(master);
  1195. acpi_register_spi_devices(master);
  1196. done:
  1197. return status;
  1198. }
  1199. EXPORT_SYMBOL_GPL(spi_register_master);
  1200. static void devm_spi_unregister(struct device *dev, void *res)
  1201. {
  1202. spi_unregister_master(*(struct spi_master **)res);
  1203. }
  1204. /**
  1205. * dev_spi_register_master - register managed SPI master controller
  1206. * @dev: device managing SPI master
  1207. * @master: initialized master, originally from spi_alloc_master()
  1208. * Context: can sleep
  1209. *
  1210. * Register a SPI device as with spi_register_master() which will
  1211. * automatically be unregister
  1212. */
  1213. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1214. {
  1215. struct spi_master **ptr;
  1216. int ret;
  1217. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1218. if (!ptr)
  1219. return -ENOMEM;
  1220. ret = spi_register_master(master);
  1221. if (!ret) {
  1222. *ptr = master;
  1223. devres_add(dev, ptr);
  1224. } else {
  1225. devres_free(ptr);
  1226. }
  1227. return ret;
  1228. }
  1229. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1230. static int __unregister(struct device *dev, void *null)
  1231. {
  1232. spi_unregister_device(to_spi_device(dev));
  1233. return 0;
  1234. }
  1235. /**
  1236. * spi_unregister_master - unregister SPI master controller
  1237. * @master: the master being unregistered
  1238. * Context: can sleep
  1239. *
  1240. * This call is used only by SPI master controller drivers, which are the
  1241. * only ones directly touching chip registers.
  1242. *
  1243. * This must be called from context that can sleep.
  1244. */
  1245. void spi_unregister_master(struct spi_master *master)
  1246. {
  1247. int dummy;
  1248. if (master->queued) {
  1249. if (spi_destroy_queue(master))
  1250. dev_err(&master->dev, "queue remove failed\n");
  1251. }
  1252. mutex_lock(&board_lock);
  1253. list_del(&master->list);
  1254. mutex_unlock(&board_lock);
  1255. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1256. device_unregister(&master->dev);
  1257. }
  1258. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1259. int spi_master_suspend(struct spi_master *master)
  1260. {
  1261. int ret;
  1262. /* Basically no-ops for non-queued masters */
  1263. if (!master->queued)
  1264. return 0;
  1265. ret = spi_stop_queue(master);
  1266. if (ret)
  1267. dev_err(&master->dev, "queue stop failed\n");
  1268. return ret;
  1269. }
  1270. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1271. int spi_master_resume(struct spi_master *master)
  1272. {
  1273. int ret;
  1274. if (!master->queued)
  1275. return 0;
  1276. ret = spi_start_queue(master);
  1277. if (ret)
  1278. dev_err(&master->dev, "queue restart failed\n");
  1279. return ret;
  1280. }
  1281. EXPORT_SYMBOL_GPL(spi_master_resume);
  1282. static int __spi_master_match(struct device *dev, const void *data)
  1283. {
  1284. struct spi_master *m;
  1285. const u16 *bus_num = data;
  1286. m = container_of(dev, struct spi_master, dev);
  1287. return m->bus_num == *bus_num;
  1288. }
  1289. /**
  1290. * spi_busnum_to_master - look up master associated with bus_num
  1291. * @bus_num: the master's bus number
  1292. * Context: can sleep
  1293. *
  1294. * This call may be used with devices that are registered after
  1295. * arch init time. It returns a refcounted pointer to the relevant
  1296. * spi_master (which the caller must release), or NULL if there is
  1297. * no such master registered.
  1298. */
  1299. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1300. {
  1301. struct device *dev;
  1302. struct spi_master *master = NULL;
  1303. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1304. __spi_master_match);
  1305. if (dev)
  1306. master = container_of(dev, struct spi_master, dev);
  1307. /* reference got in class_find_device */
  1308. return master;
  1309. }
  1310. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1311. /*-------------------------------------------------------------------------*/
  1312. /* Core methods for SPI master protocol drivers. Some of the
  1313. * other core methods are currently defined as inline functions.
  1314. */
  1315. /**
  1316. * spi_setup - setup SPI mode and clock rate
  1317. * @spi: the device whose settings are being modified
  1318. * Context: can sleep, and no requests are queued to the device
  1319. *
  1320. * SPI protocol drivers may need to update the transfer mode if the
  1321. * device doesn't work with its default. They may likewise need
  1322. * to update clock rates or word sizes from initial values. This function
  1323. * changes those settings, and must be called from a context that can sleep.
  1324. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1325. * effect the next time the device is selected and data is transferred to
  1326. * or from it. When this function returns, the spi device is deselected.
  1327. *
  1328. * Note that this call will fail if the protocol driver specifies an option
  1329. * that the underlying controller or its driver does not support. For
  1330. * example, not all hardware supports wire transfers using nine bit words,
  1331. * LSB-first wire encoding, or active-high chipselects.
  1332. */
  1333. int spi_setup(struct spi_device *spi)
  1334. {
  1335. unsigned bad_bits;
  1336. int status = 0;
  1337. /* check mode to prevent that DUAL and QUAD set at the same time
  1338. */
  1339. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  1340. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  1341. dev_err(&spi->dev,
  1342. "setup: can not select dual and quad at the same time\n");
  1343. return -EINVAL;
  1344. }
  1345. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  1346. */
  1347. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  1348. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  1349. return -EINVAL;
  1350. /* help drivers fail *cleanly* when they need options
  1351. * that aren't supported with their current master
  1352. */
  1353. bad_bits = spi->mode & ~spi->master->mode_bits;
  1354. if (bad_bits) {
  1355. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1356. bad_bits);
  1357. return -EINVAL;
  1358. }
  1359. if (!spi->bits_per_word)
  1360. spi->bits_per_word = 8;
  1361. if (spi->master->setup)
  1362. status = spi->master->setup(spi);
  1363. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  1364. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1365. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1366. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1367. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1368. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1369. spi->bits_per_word, spi->max_speed_hz,
  1370. status);
  1371. return status;
  1372. }
  1373. EXPORT_SYMBOL_GPL(spi_setup);
  1374. static int __spi_validate(struct spi_device *spi, struct spi_message *message)
  1375. {
  1376. struct spi_master *master = spi->master;
  1377. struct spi_transfer *xfer;
  1378. if (list_empty(&message->transfers))
  1379. return -EINVAL;
  1380. if (!message->complete)
  1381. return -EINVAL;
  1382. /* Half-duplex links include original MicroWire, and ones with
  1383. * only one data pin like SPI_3WIRE (switches direction) or where
  1384. * either MOSI or MISO is missing. They can also be caused by
  1385. * software limitations.
  1386. */
  1387. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1388. || (spi->mode & SPI_3WIRE)) {
  1389. unsigned flags = master->flags;
  1390. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1391. if (xfer->rx_buf && xfer->tx_buf)
  1392. return -EINVAL;
  1393. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1394. return -EINVAL;
  1395. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1396. return -EINVAL;
  1397. }
  1398. }
  1399. /**
  1400. * Set transfer bits_per_word and max speed as spi device default if
  1401. * it is not set for this transfer.
  1402. * Set transfer tx_nbits and rx_nbits as single transfer default
  1403. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  1404. */
  1405. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1406. message->frame_length += xfer->len;
  1407. if (!xfer->bits_per_word)
  1408. xfer->bits_per_word = spi->bits_per_word;
  1409. if (!xfer->speed_hz) {
  1410. xfer->speed_hz = spi->max_speed_hz;
  1411. if (master->max_speed_hz &&
  1412. xfer->speed_hz > master->max_speed_hz)
  1413. xfer->speed_hz = master->max_speed_hz;
  1414. }
  1415. if (master->bits_per_word_mask) {
  1416. /* Only 32 bits fit in the mask */
  1417. if (xfer->bits_per_word > 32)
  1418. return -EINVAL;
  1419. if (!(master->bits_per_word_mask &
  1420. BIT(xfer->bits_per_word - 1)))
  1421. return -EINVAL;
  1422. }
  1423. if (xfer->speed_hz && master->min_speed_hz &&
  1424. xfer->speed_hz < master->min_speed_hz)
  1425. return -EINVAL;
  1426. if (xfer->speed_hz && master->max_speed_hz &&
  1427. xfer->speed_hz > master->max_speed_hz)
  1428. return -EINVAL;
  1429. if (xfer->tx_buf && !xfer->tx_nbits)
  1430. xfer->tx_nbits = SPI_NBITS_SINGLE;
  1431. if (xfer->rx_buf && !xfer->rx_nbits)
  1432. xfer->rx_nbits = SPI_NBITS_SINGLE;
  1433. /* check transfer tx/rx_nbits:
  1434. * 1. check the value matches one of single, dual and quad
  1435. * 2. check tx/rx_nbits match the mode in spi_device
  1436. */
  1437. if (xfer->tx_buf) {
  1438. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  1439. xfer->tx_nbits != SPI_NBITS_DUAL &&
  1440. xfer->tx_nbits != SPI_NBITS_QUAD)
  1441. return -EINVAL;
  1442. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  1443. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  1444. return -EINVAL;
  1445. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  1446. !(spi->mode & SPI_TX_QUAD))
  1447. return -EINVAL;
  1448. }
  1449. /* check transfer rx_nbits */
  1450. if (xfer->rx_buf) {
  1451. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  1452. xfer->rx_nbits != SPI_NBITS_DUAL &&
  1453. xfer->rx_nbits != SPI_NBITS_QUAD)
  1454. return -EINVAL;
  1455. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  1456. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  1457. return -EINVAL;
  1458. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  1459. !(spi->mode & SPI_RX_QUAD))
  1460. return -EINVAL;
  1461. }
  1462. }
  1463. message->status = -EINPROGRESS;
  1464. return 0;
  1465. }
  1466. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1467. {
  1468. struct spi_master *master = spi->master;
  1469. message->spi = spi;
  1470. trace_spi_message_submit(message);
  1471. return master->transfer(spi, message);
  1472. }
  1473. /**
  1474. * spi_async - asynchronous SPI transfer
  1475. * @spi: device with which data will be exchanged
  1476. * @message: describes the data transfers, including completion callback
  1477. * Context: any (irqs may be blocked, etc)
  1478. *
  1479. * This call may be used in_irq and other contexts which can't sleep,
  1480. * as well as from task contexts which can sleep.
  1481. *
  1482. * The completion callback is invoked in a context which can't sleep.
  1483. * Before that invocation, the value of message->status is undefined.
  1484. * When the callback is issued, message->status holds either zero (to
  1485. * indicate complete success) or a negative error code. After that
  1486. * callback returns, the driver which issued the transfer request may
  1487. * deallocate the associated memory; it's no longer in use by any SPI
  1488. * core or controller driver code.
  1489. *
  1490. * Note that although all messages to a spi_device are handled in
  1491. * FIFO order, messages may go to different devices in other orders.
  1492. * Some device might be higher priority, or have various "hard" access
  1493. * time requirements, for example.
  1494. *
  1495. * On detection of any fault during the transfer, processing of
  1496. * the entire message is aborted, and the device is deselected.
  1497. * Until returning from the associated message completion callback,
  1498. * no other spi_message queued to that device will be processed.
  1499. * (This rule applies equally to all the synchronous transfer calls,
  1500. * which are wrappers around this core asynchronous primitive.)
  1501. */
  1502. int spi_async(struct spi_device *spi, struct spi_message *message)
  1503. {
  1504. struct spi_master *master = spi->master;
  1505. int ret;
  1506. unsigned long flags;
  1507. ret = __spi_validate(spi, message);
  1508. if (ret != 0)
  1509. return ret;
  1510. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1511. if (master->bus_lock_flag)
  1512. ret = -EBUSY;
  1513. else
  1514. ret = __spi_async(spi, message);
  1515. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1516. return ret;
  1517. }
  1518. EXPORT_SYMBOL_GPL(spi_async);
  1519. /**
  1520. * spi_async_locked - version of spi_async with exclusive bus usage
  1521. * @spi: device with which data will be exchanged
  1522. * @message: describes the data transfers, including completion callback
  1523. * Context: any (irqs may be blocked, etc)
  1524. *
  1525. * This call may be used in_irq and other contexts which can't sleep,
  1526. * as well as from task contexts which can sleep.
  1527. *
  1528. * The completion callback is invoked in a context which can't sleep.
  1529. * Before that invocation, the value of message->status is undefined.
  1530. * When the callback is issued, message->status holds either zero (to
  1531. * indicate complete success) or a negative error code. After that
  1532. * callback returns, the driver which issued the transfer request may
  1533. * deallocate the associated memory; it's no longer in use by any SPI
  1534. * core or controller driver code.
  1535. *
  1536. * Note that although all messages to a spi_device are handled in
  1537. * FIFO order, messages may go to different devices in other orders.
  1538. * Some device might be higher priority, or have various "hard" access
  1539. * time requirements, for example.
  1540. *
  1541. * On detection of any fault during the transfer, processing of
  1542. * the entire message is aborted, and the device is deselected.
  1543. * Until returning from the associated message completion callback,
  1544. * no other spi_message queued to that device will be processed.
  1545. * (This rule applies equally to all the synchronous transfer calls,
  1546. * which are wrappers around this core asynchronous primitive.)
  1547. */
  1548. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1549. {
  1550. struct spi_master *master = spi->master;
  1551. int ret;
  1552. unsigned long flags;
  1553. ret = __spi_validate(spi, message);
  1554. if (ret != 0)
  1555. return ret;
  1556. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1557. ret = __spi_async(spi, message);
  1558. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1559. return ret;
  1560. }
  1561. EXPORT_SYMBOL_GPL(spi_async_locked);
  1562. /*-------------------------------------------------------------------------*/
  1563. /* Utility methods for SPI master protocol drivers, layered on
  1564. * top of the core. Some other utility methods are defined as
  1565. * inline functions.
  1566. */
  1567. static void spi_complete(void *arg)
  1568. {
  1569. complete(arg);
  1570. }
  1571. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1572. int bus_locked)
  1573. {
  1574. DECLARE_COMPLETION_ONSTACK(done);
  1575. int status;
  1576. struct spi_master *master = spi->master;
  1577. message->complete = spi_complete;
  1578. message->context = &done;
  1579. if (!bus_locked)
  1580. mutex_lock(&master->bus_lock_mutex);
  1581. status = spi_async_locked(spi, message);
  1582. if (!bus_locked)
  1583. mutex_unlock(&master->bus_lock_mutex);
  1584. if (status == 0) {
  1585. wait_for_completion(&done);
  1586. status = message->status;
  1587. }
  1588. message->context = NULL;
  1589. return status;
  1590. }
  1591. /**
  1592. * spi_sync - blocking/synchronous SPI data transfers
  1593. * @spi: device with which data will be exchanged
  1594. * @message: describes the data transfers
  1595. * Context: can sleep
  1596. *
  1597. * This call may only be used from a context that may sleep. The sleep
  1598. * is non-interruptible, and has no timeout. Low-overhead controller
  1599. * drivers may DMA directly into and out of the message buffers.
  1600. *
  1601. * Note that the SPI device's chip select is active during the message,
  1602. * and then is normally disabled between messages. Drivers for some
  1603. * frequently-used devices may want to minimize costs of selecting a chip,
  1604. * by leaving it selected in anticipation that the next message will go
  1605. * to the same chip. (That may increase power usage.)
  1606. *
  1607. * Also, the caller is guaranteeing that the memory associated with the
  1608. * message will not be freed before this call returns.
  1609. *
  1610. * It returns zero on success, else a negative error code.
  1611. */
  1612. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1613. {
  1614. return __spi_sync(spi, message, 0);
  1615. }
  1616. EXPORT_SYMBOL_GPL(spi_sync);
  1617. /**
  1618. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1619. * @spi: device with which data will be exchanged
  1620. * @message: describes the data transfers
  1621. * Context: can sleep
  1622. *
  1623. * This call may only be used from a context that may sleep. The sleep
  1624. * is non-interruptible, and has no timeout. Low-overhead controller
  1625. * drivers may DMA directly into and out of the message buffers.
  1626. *
  1627. * This call should be used by drivers that require exclusive access to the
  1628. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1629. * be released by a spi_bus_unlock call when the exclusive access is over.
  1630. *
  1631. * It returns zero on success, else a negative error code.
  1632. */
  1633. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1634. {
  1635. return __spi_sync(spi, message, 1);
  1636. }
  1637. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1638. /**
  1639. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1640. * @master: SPI bus master that should be locked for exclusive bus access
  1641. * Context: can sleep
  1642. *
  1643. * This call may only be used from a context that may sleep. The sleep
  1644. * is non-interruptible, and has no timeout.
  1645. *
  1646. * This call should be used by drivers that require exclusive access to the
  1647. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1648. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1649. * and spi_async_locked calls when the SPI bus lock is held.
  1650. *
  1651. * It returns zero on success, else a negative error code.
  1652. */
  1653. int spi_bus_lock(struct spi_master *master)
  1654. {
  1655. unsigned long flags;
  1656. mutex_lock(&master->bus_lock_mutex);
  1657. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1658. master->bus_lock_flag = 1;
  1659. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1660. /* mutex remains locked until spi_bus_unlock is called */
  1661. return 0;
  1662. }
  1663. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1664. /**
  1665. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1666. * @master: SPI bus master that was locked for exclusive bus access
  1667. * Context: can sleep
  1668. *
  1669. * This call may only be used from a context that may sleep. The sleep
  1670. * is non-interruptible, and has no timeout.
  1671. *
  1672. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1673. * call.
  1674. *
  1675. * It returns zero on success, else a negative error code.
  1676. */
  1677. int spi_bus_unlock(struct spi_master *master)
  1678. {
  1679. master->bus_lock_flag = 0;
  1680. mutex_unlock(&master->bus_lock_mutex);
  1681. return 0;
  1682. }
  1683. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1684. /* portable code must never pass more than 32 bytes */
  1685. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  1686. static u8 *buf;
  1687. /**
  1688. * spi_write_then_read - SPI synchronous write followed by read
  1689. * @spi: device with which data will be exchanged
  1690. * @txbuf: data to be written (need not be dma-safe)
  1691. * @n_tx: size of txbuf, in bytes
  1692. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1693. * @n_rx: size of rxbuf, in bytes
  1694. * Context: can sleep
  1695. *
  1696. * This performs a half duplex MicroWire style transaction with the
  1697. * device, sending txbuf and then reading rxbuf. The return value
  1698. * is zero for success, else a negative errno status code.
  1699. * This call may only be used from a context that may sleep.
  1700. *
  1701. * Parameters to this routine are always copied using a small buffer;
  1702. * portable code should never use this for more than 32 bytes.
  1703. * Performance-sensitive or bulk transfer code should instead use
  1704. * spi_{async,sync}() calls with dma-safe buffers.
  1705. */
  1706. int spi_write_then_read(struct spi_device *spi,
  1707. const void *txbuf, unsigned n_tx,
  1708. void *rxbuf, unsigned n_rx)
  1709. {
  1710. static DEFINE_MUTEX(lock);
  1711. int status;
  1712. struct spi_message message;
  1713. struct spi_transfer x[2];
  1714. u8 *local_buf;
  1715. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1716. * copying here, (as a pure convenience thing), but we can
  1717. * keep heap costs out of the hot path unless someone else is
  1718. * using the pre-allocated buffer or the transfer is too large.
  1719. */
  1720. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1721. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1722. GFP_KERNEL | GFP_DMA);
  1723. if (!local_buf)
  1724. return -ENOMEM;
  1725. } else {
  1726. local_buf = buf;
  1727. }
  1728. spi_message_init(&message);
  1729. memset(x, 0, sizeof(x));
  1730. if (n_tx) {
  1731. x[0].len = n_tx;
  1732. spi_message_add_tail(&x[0], &message);
  1733. }
  1734. if (n_rx) {
  1735. x[1].len = n_rx;
  1736. spi_message_add_tail(&x[1], &message);
  1737. }
  1738. memcpy(local_buf, txbuf, n_tx);
  1739. x[0].tx_buf = local_buf;
  1740. x[1].rx_buf = local_buf + n_tx;
  1741. /* do the i/o */
  1742. status = spi_sync(spi, &message);
  1743. if (status == 0)
  1744. memcpy(rxbuf, x[1].rx_buf, n_rx);
  1745. if (x[0].tx_buf == buf)
  1746. mutex_unlock(&lock);
  1747. else
  1748. kfree(local_buf);
  1749. return status;
  1750. }
  1751. EXPORT_SYMBOL_GPL(spi_write_then_read);
  1752. /*-------------------------------------------------------------------------*/
  1753. static int __init spi_init(void)
  1754. {
  1755. int status;
  1756. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  1757. if (!buf) {
  1758. status = -ENOMEM;
  1759. goto err0;
  1760. }
  1761. status = bus_register(&spi_bus_type);
  1762. if (status < 0)
  1763. goto err1;
  1764. status = class_register(&spi_master_class);
  1765. if (status < 0)
  1766. goto err2;
  1767. return 0;
  1768. err2:
  1769. bus_unregister(&spi_bus_type);
  1770. err1:
  1771. kfree(buf);
  1772. buf = NULL;
  1773. err0:
  1774. return status;
  1775. }
  1776. /* board_info is normally registered in arch_initcall(),
  1777. * but even essential drivers wait till later
  1778. *
  1779. * REVISIT only boardinfo really needs static linking. the rest (device and
  1780. * driver registration) _could_ be dynamically linked (modular) ... costs
  1781. * include needing to have boardinfo data structures be much more public.
  1782. */
  1783. postcore_initcall(spi_init);