spi.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509
  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. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/cache.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/mutex.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/clk/clk-conf.h>
  27. #include <linux/slab.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/of_gpio.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/pm_domain.h>
  33. #include <linux/export.h>
  34. #include <linux/sched/rt.h>
  35. #include <linux/delay.h>
  36. #include <linux/kthread.h>
  37. #include <linux/ioport.h>
  38. #include <linux/acpi.h>
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/spi.h>
  41. static void spidev_release(struct device *dev)
  42. {
  43. struct spi_device *spi = to_spi_device(dev);
  44. /* spi masters may cleanup for released devices */
  45. if (spi->master->cleanup)
  46. spi->master->cleanup(spi);
  47. spi_master_put(spi->master);
  48. kfree(spi);
  49. }
  50. static ssize_t
  51. modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  52. {
  53. const struct spi_device *spi = to_spi_device(dev);
  54. int len;
  55. len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
  56. if (len != -ENODEV)
  57. return len;
  58. return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
  59. }
  60. static DEVICE_ATTR_RO(modalias);
  61. static struct attribute *spi_dev_attrs[] = {
  62. &dev_attr_modalias.attr,
  63. NULL,
  64. };
  65. ATTRIBUTE_GROUPS(spi_dev);
  66. /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
  67. * and the sysfs version makes coldplug work too.
  68. */
  69. static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
  70. const struct spi_device *sdev)
  71. {
  72. while (id->name[0]) {
  73. if (!strcmp(sdev->modalias, id->name))
  74. return id;
  75. id++;
  76. }
  77. return NULL;
  78. }
  79. const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
  80. {
  81. const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
  82. return spi_match_id(sdrv->id_table, sdev);
  83. }
  84. EXPORT_SYMBOL_GPL(spi_get_device_id);
  85. static int spi_match_device(struct device *dev, struct device_driver *drv)
  86. {
  87. const struct spi_device *spi = to_spi_device(dev);
  88. const struct spi_driver *sdrv = to_spi_driver(drv);
  89. /* Attempt an OF style match */
  90. if (of_driver_match_device(dev, drv))
  91. return 1;
  92. /* Then try ACPI */
  93. if (acpi_driver_match_device(dev, drv))
  94. return 1;
  95. if (sdrv->id_table)
  96. return !!spi_match_id(sdrv->id_table, spi);
  97. return strcmp(spi->modalias, drv->name) == 0;
  98. }
  99. static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
  100. {
  101. const struct spi_device *spi = to_spi_device(dev);
  102. int rc;
  103. rc = acpi_device_uevent_modalias(dev, env);
  104. if (rc != -ENODEV)
  105. return rc;
  106. add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
  107. return 0;
  108. }
  109. #ifdef CONFIG_PM_SLEEP
  110. static int spi_legacy_suspend(struct device *dev, pm_message_t message)
  111. {
  112. int value = 0;
  113. struct spi_driver *drv = to_spi_driver(dev->driver);
  114. /* suspend will stop irqs and dma; no more i/o */
  115. if (drv) {
  116. if (drv->suspend)
  117. value = drv->suspend(to_spi_device(dev), message);
  118. else
  119. dev_dbg(dev, "... can't suspend\n");
  120. }
  121. return value;
  122. }
  123. static int spi_legacy_resume(struct device *dev)
  124. {
  125. int value = 0;
  126. struct spi_driver *drv = to_spi_driver(dev->driver);
  127. /* resume may restart the i/o queue */
  128. if (drv) {
  129. if (drv->resume)
  130. value = drv->resume(to_spi_device(dev));
  131. else
  132. dev_dbg(dev, "... can't resume\n");
  133. }
  134. return value;
  135. }
  136. static int spi_pm_suspend(struct device *dev)
  137. {
  138. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  139. if (pm)
  140. return pm_generic_suspend(dev);
  141. else
  142. return spi_legacy_suspend(dev, PMSG_SUSPEND);
  143. }
  144. static int spi_pm_resume(struct device *dev)
  145. {
  146. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  147. if (pm)
  148. return pm_generic_resume(dev);
  149. else
  150. return spi_legacy_resume(dev);
  151. }
  152. static int spi_pm_freeze(struct device *dev)
  153. {
  154. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  155. if (pm)
  156. return pm_generic_freeze(dev);
  157. else
  158. return spi_legacy_suspend(dev, PMSG_FREEZE);
  159. }
  160. static int spi_pm_thaw(struct device *dev)
  161. {
  162. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  163. if (pm)
  164. return pm_generic_thaw(dev);
  165. else
  166. return spi_legacy_resume(dev);
  167. }
  168. static int spi_pm_poweroff(struct device *dev)
  169. {
  170. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  171. if (pm)
  172. return pm_generic_poweroff(dev);
  173. else
  174. return spi_legacy_suspend(dev, PMSG_HIBERNATE);
  175. }
  176. static int spi_pm_restore(struct device *dev)
  177. {
  178. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  179. if (pm)
  180. return pm_generic_restore(dev);
  181. else
  182. return spi_legacy_resume(dev);
  183. }
  184. #else
  185. #define spi_pm_suspend NULL
  186. #define spi_pm_resume NULL
  187. #define spi_pm_freeze NULL
  188. #define spi_pm_thaw NULL
  189. #define spi_pm_poweroff NULL
  190. #define spi_pm_restore NULL
  191. #endif
  192. static const struct dev_pm_ops spi_pm = {
  193. .suspend = spi_pm_suspend,
  194. .resume = spi_pm_resume,
  195. .freeze = spi_pm_freeze,
  196. .thaw = spi_pm_thaw,
  197. .poweroff = spi_pm_poweroff,
  198. .restore = spi_pm_restore,
  199. SET_RUNTIME_PM_OPS(
  200. pm_generic_runtime_suspend,
  201. pm_generic_runtime_resume,
  202. NULL
  203. )
  204. };
  205. struct bus_type spi_bus_type = {
  206. .name = "spi",
  207. .dev_groups = spi_dev_groups,
  208. .match = spi_match_device,
  209. .uevent = spi_uevent,
  210. .pm = &spi_pm,
  211. };
  212. EXPORT_SYMBOL_GPL(spi_bus_type);
  213. static int spi_drv_probe(struct device *dev)
  214. {
  215. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  216. int ret;
  217. ret = of_clk_set_defaults(dev->of_node, false);
  218. if (ret)
  219. return ret;
  220. ret = dev_pm_domain_attach(dev, true);
  221. if (ret != -EPROBE_DEFER) {
  222. ret = sdrv->probe(to_spi_device(dev));
  223. if (ret)
  224. dev_pm_domain_detach(dev, true);
  225. }
  226. return ret;
  227. }
  228. static int spi_drv_remove(struct device *dev)
  229. {
  230. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  231. int ret;
  232. ret = sdrv->remove(to_spi_device(dev));
  233. dev_pm_domain_detach(dev, true);
  234. return ret;
  235. }
  236. static void spi_drv_shutdown(struct device *dev)
  237. {
  238. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  239. sdrv->shutdown(to_spi_device(dev));
  240. }
  241. /**
  242. * spi_register_driver - register a SPI driver
  243. * @sdrv: the driver to register
  244. * Context: can sleep
  245. */
  246. int spi_register_driver(struct spi_driver *sdrv)
  247. {
  248. sdrv->driver.bus = &spi_bus_type;
  249. if (sdrv->probe)
  250. sdrv->driver.probe = spi_drv_probe;
  251. if (sdrv->remove)
  252. sdrv->driver.remove = spi_drv_remove;
  253. if (sdrv->shutdown)
  254. sdrv->driver.shutdown = spi_drv_shutdown;
  255. return driver_register(&sdrv->driver);
  256. }
  257. EXPORT_SYMBOL_GPL(spi_register_driver);
  258. /*-------------------------------------------------------------------------*/
  259. /* SPI devices should normally not be created by SPI device drivers; that
  260. * would make them board-specific. Similarly with SPI master drivers.
  261. * Device registration normally goes into like arch/.../mach.../board-YYY.c
  262. * with other readonly (flashable) information about mainboard devices.
  263. */
  264. struct boardinfo {
  265. struct list_head list;
  266. struct spi_board_info board_info;
  267. };
  268. static LIST_HEAD(board_list);
  269. static LIST_HEAD(spi_master_list);
  270. /*
  271. * Used to protect add/del opertion for board_info list and
  272. * spi_master list, and their matching process
  273. */
  274. static DEFINE_MUTEX(board_lock);
  275. /**
  276. * spi_alloc_device - Allocate a new SPI device
  277. * @master: Controller to which device is connected
  278. * Context: can sleep
  279. *
  280. * Allows a driver to allocate and initialize a spi_device without
  281. * registering it immediately. This allows a driver to directly
  282. * fill the spi_device with device parameters before calling
  283. * spi_add_device() on it.
  284. *
  285. * Caller is responsible to call spi_add_device() on the returned
  286. * spi_device structure to add it to the SPI master. If the caller
  287. * needs to discard the spi_device without adding it, then it should
  288. * call spi_dev_put() on it.
  289. *
  290. * Returns a pointer to the new device, or NULL.
  291. */
  292. struct spi_device *spi_alloc_device(struct spi_master *master)
  293. {
  294. struct spi_device *spi;
  295. if (!spi_master_get(master))
  296. return NULL;
  297. spi = kzalloc(sizeof(*spi), GFP_KERNEL);
  298. if (!spi) {
  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. if (!n)
  469. return -EINVAL;
  470. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  471. if (!bi)
  472. return -ENOMEM;
  473. for (i = 0; i < n; i++, bi++, info++) {
  474. struct spi_master *master;
  475. memcpy(&bi->board_info, info, sizeof(*info));
  476. mutex_lock(&board_lock);
  477. list_add_tail(&bi->list, &board_list);
  478. list_for_each_entry(master, &spi_master_list, list)
  479. spi_match_master_to_boardinfo(master, &bi->board_info);
  480. mutex_unlock(&board_lock);
  481. }
  482. return 0;
  483. }
  484. /*-------------------------------------------------------------------------*/
  485. static void spi_set_cs(struct spi_device *spi, bool enable)
  486. {
  487. if (spi->mode & SPI_CS_HIGH)
  488. enable = !enable;
  489. if (spi->cs_gpio >= 0)
  490. gpio_set_value(spi->cs_gpio, !enable);
  491. else if (spi->master->set_cs)
  492. spi->master->set_cs(spi, !enable);
  493. }
  494. #ifdef CONFIG_HAS_DMA
  495. static int spi_map_buf(struct spi_master *master, struct device *dev,
  496. struct sg_table *sgt, void *buf, size_t len,
  497. enum dma_data_direction dir)
  498. {
  499. const bool vmalloced_buf = is_vmalloc_addr(buf);
  500. const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len;
  501. const int sgs = DIV_ROUND_UP(len, desc_len);
  502. struct page *vm_page;
  503. void *sg_buf;
  504. size_t min;
  505. int i, ret;
  506. ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
  507. if (ret != 0)
  508. return ret;
  509. for (i = 0; i < sgs; i++) {
  510. min = min_t(size_t, len, desc_len);
  511. if (vmalloced_buf) {
  512. vm_page = vmalloc_to_page(buf);
  513. if (!vm_page) {
  514. sg_free_table(sgt);
  515. return -ENOMEM;
  516. }
  517. sg_set_page(&sgt->sgl[i], vm_page,
  518. min, offset_in_page(buf));
  519. } else {
  520. sg_buf = buf;
  521. sg_set_buf(&sgt->sgl[i], sg_buf, min);
  522. }
  523. buf += min;
  524. len -= min;
  525. }
  526. ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
  527. if (!ret)
  528. ret = -ENOMEM;
  529. if (ret < 0) {
  530. sg_free_table(sgt);
  531. return ret;
  532. }
  533. sgt->nents = ret;
  534. return 0;
  535. }
  536. static void spi_unmap_buf(struct spi_master *master, struct device *dev,
  537. struct sg_table *sgt, enum dma_data_direction dir)
  538. {
  539. if (sgt->orig_nents) {
  540. dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
  541. sg_free_table(sgt);
  542. }
  543. }
  544. static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
  545. {
  546. struct device *tx_dev, *rx_dev;
  547. struct spi_transfer *xfer;
  548. int ret;
  549. if (!master->can_dma)
  550. return 0;
  551. tx_dev = master->dma_tx->device->dev;
  552. rx_dev = master->dma_rx->device->dev;
  553. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  554. if (!master->can_dma(master, msg->spi, xfer))
  555. continue;
  556. if (xfer->tx_buf != NULL) {
  557. ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
  558. (void *)xfer->tx_buf, xfer->len,
  559. DMA_TO_DEVICE);
  560. if (ret != 0)
  561. return ret;
  562. }
  563. if (xfer->rx_buf != NULL) {
  564. ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
  565. xfer->rx_buf, xfer->len,
  566. DMA_FROM_DEVICE);
  567. if (ret != 0) {
  568. spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
  569. DMA_TO_DEVICE);
  570. return ret;
  571. }
  572. }
  573. }
  574. master->cur_msg_mapped = true;
  575. return 0;
  576. }
  577. static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
  578. {
  579. struct spi_transfer *xfer;
  580. struct device *tx_dev, *rx_dev;
  581. if (!master->cur_msg_mapped || !master->can_dma)
  582. return 0;
  583. tx_dev = master->dma_tx->device->dev;
  584. rx_dev = master->dma_rx->device->dev;
  585. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  586. if (!master->can_dma(master, msg->spi, xfer))
  587. continue;
  588. spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
  589. spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
  590. }
  591. return 0;
  592. }
  593. #else /* !CONFIG_HAS_DMA */
  594. static inline int __spi_map_msg(struct spi_master *master,
  595. struct spi_message *msg)
  596. {
  597. return 0;
  598. }
  599. static inline int spi_unmap_msg(struct spi_master *master,
  600. struct spi_message *msg)
  601. {
  602. return 0;
  603. }
  604. #endif /* !CONFIG_HAS_DMA */
  605. static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
  606. {
  607. struct spi_transfer *xfer;
  608. void *tmp;
  609. unsigned int max_tx, max_rx;
  610. if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
  611. max_tx = 0;
  612. max_rx = 0;
  613. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  614. if ((master->flags & SPI_MASTER_MUST_TX) &&
  615. !xfer->tx_buf)
  616. max_tx = max(xfer->len, max_tx);
  617. if ((master->flags & SPI_MASTER_MUST_RX) &&
  618. !xfer->rx_buf)
  619. max_rx = max(xfer->len, max_rx);
  620. }
  621. if (max_tx) {
  622. tmp = krealloc(master->dummy_tx, max_tx,
  623. GFP_KERNEL | GFP_DMA);
  624. if (!tmp)
  625. return -ENOMEM;
  626. master->dummy_tx = tmp;
  627. memset(tmp, 0, max_tx);
  628. }
  629. if (max_rx) {
  630. tmp = krealloc(master->dummy_rx, max_rx,
  631. GFP_KERNEL | GFP_DMA);
  632. if (!tmp)
  633. return -ENOMEM;
  634. master->dummy_rx = tmp;
  635. }
  636. if (max_tx || max_rx) {
  637. list_for_each_entry(xfer, &msg->transfers,
  638. transfer_list) {
  639. if (!xfer->tx_buf)
  640. xfer->tx_buf = master->dummy_tx;
  641. if (!xfer->rx_buf)
  642. xfer->rx_buf = master->dummy_rx;
  643. }
  644. }
  645. }
  646. return __spi_map_msg(master, msg);
  647. }
  648. /*
  649. * spi_transfer_one_message - Default implementation of transfer_one_message()
  650. *
  651. * This is a standard implementation of transfer_one_message() for
  652. * drivers which impelment a transfer_one() operation. It provides
  653. * standard handling of delays and chip select management.
  654. */
  655. static int spi_transfer_one_message(struct spi_master *master,
  656. struct spi_message *msg)
  657. {
  658. struct spi_transfer *xfer;
  659. bool keep_cs = false;
  660. int ret = 0;
  661. unsigned long ms = 1;
  662. spi_set_cs(msg->spi, true);
  663. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  664. trace_spi_transfer_start(msg, xfer);
  665. if (xfer->tx_buf || xfer->rx_buf) {
  666. reinit_completion(&master->xfer_completion);
  667. ret = master->transfer_one(master, msg->spi, xfer);
  668. if (ret < 0) {
  669. dev_err(&msg->spi->dev,
  670. "SPI transfer failed: %d\n", ret);
  671. goto out;
  672. }
  673. if (ret > 0) {
  674. ret = 0;
  675. ms = xfer->len * 8 * 1000 / xfer->speed_hz;
  676. ms += ms + 100; /* some tolerance */
  677. ms = wait_for_completion_timeout(&master->xfer_completion,
  678. msecs_to_jiffies(ms));
  679. }
  680. if (ms == 0) {
  681. dev_err(&msg->spi->dev,
  682. "SPI transfer timed out\n");
  683. msg->status = -ETIMEDOUT;
  684. }
  685. } else {
  686. if (xfer->len)
  687. dev_err(&msg->spi->dev,
  688. "Bufferless transfer has length %u\n",
  689. xfer->len);
  690. }
  691. trace_spi_transfer_stop(msg, xfer);
  692. if (msg->status != -EINPROGRESS)
  693. goto out;
  694. if (xfer->delay_usecs)
  695. udelay(xfer->delay_usecs);
  696. if (xfer->cs_change) {
  697. if (list_is_last(&xfer->transfer_list,
  698. &msg->transfers)) {
  699. keep_cs = true;
  700. } else {
  701. spi_set_cs(msg->spi, false);
  702. udelay(10);
  703. spi_set_cs(msg->spi, true);
  704. }
  705. }
  706. msg->actual_length += xfer->len;
  707. }
  708. out:
  709. if (ret != 0 || !keep_cs)
  710. spi_set_cs(msg->spi, false);
  711. if (msg->status == -EINPROGRESS)
  712. msg->status = ret;
  713. spi_finalize_current_message(master);
  714. return ret;
  715. }
  716. /**
  717. * spi_finalize_current_transfer - report completion of a transfer
  718. * @master: the master reporting completion
  719. *
  720. * Called by SPI drivers using the core transfer_one_message()
  721. * implementation to notify it that the current interrupt driven
  722. * transfer has finished and the next one may be scheduled.
  723. */
  724. void spi_finalize_current_transfer(struct spi_master *master)
  725. {
  726. complete(&master->xfer_completion);
  727. }
  728. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  729. /**
  730. * __spi_pump_messages - function which processes spi message queue
  731. * @master: master to process queue for
  732. * @in_kthread: true if we are in the context of the message pump thread
  733. *
  734. * This function checks if there is any spi message in the queue that
  735. * needs processing and if so call out to the driver to initialize hardware
  736. * and transfer each message.
  737. *
  738. * Note that it is called both from the kthread itself and also from
  739. * inside spi_sync(); the queue extraction handling at the top of the
  740. * function should deal with this safely.
  741. */
  742. static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
  743. {
  744. unsigned long flags;
  745. bool was_busy = false;
  746. int ret;
  747. /* Lock queue */
  748. spin_lock_irqsave(&master->queue_lock, flags);
  749. /* Make sure we are not already running a message */
  750. if (master->cur_msg) {
  751. spin_unlock_irqrestore(&master->queue_lock, flags);
  752. return;
  753. }
  754. /* If another context is idling the device then defer */
  755. if (master->idling) {
  756. queue_kthread_work(&master->kworker, &master->pump_messages);
  757. spin_unlock_irqrestore(&master->queue_lock, flags);
  758. return;
  759. }
  760. /* Check if the queue is idle */
  761. if (list_empty(&master->queue) || !master->running) {
  762. if (!master->busy) {
  763. spin_unlock_irqrestore(&master->queue_lock, flags);
  764. return;
  765. }
  766. /* Only do teardown in the thread */
  767. if (!in_kthread) {
  768. queue_kthread_work(&master->kworker,
  769. &master->pump_messages);
  770. spin_unlock_irqrestore(&master->queue_lock, flags);
  771. return;
  772. }
  773. master->busy = false;
  774. master->idling = true;
  775. spin_unlock_irqrestore(&master->queue_lock, flags);
  776. kfree(master->dummy_rx);
  777. master->dummy_rx = NULL;
  778. kfree(master->dummy_tx);
  779. master->dummy_tx = NULL;
  780. if (master->unprepare_transfer_hardware &&
  781. master->unprepare_transfer_hardware(master))
  782. dev_err(&master->dev,
  783. "failed to unprepare transfer hardware\n");
  784. if (master->auto_runtime_pm) {
  785. pm_runtime_mark_last_busy(master->dev.parent);
  786. pm_runtime_put_autosuspend(master->dev.parent);
  787. }
  788. trace_spi_master_idle(master);
  789. spin_lock_irqsave(&master->queue_lock, flags);
  790. master->idling = false;
  791. spin_unlock_irqrestore(&master->queue_lock, flags);
  792. return;
  793. }
  794. /* Extract head of queue */
  795. master->cur_msg =
  796. list_first_entry(&master->queue, struct spi_message, queue);
  797. list_del_init(&master->cur_msg->queue);
  798. if (master->busy)
  799. was_busy = true;
  800. else
  801. master->busy = true;
  802. spin_unlock_irqrestore(&master->queue_lock, flags);
  803. if (!was_busy && master->auto_runtime_pm) {
  804. ret = pm_runtime_get_sync(master->dev.parent);
  805. if (ret < 0) {
  806. dev_err(&master->dev, "Failed to power device: %d\n",
  807. ret);
  808. return;
  809. }
  810. }
  811. if (!was_busy)
  812. trace_spi_master_busy(master);
  813. if (!was_busy && master->prepare_transfer_hardware) {
  814. ret = master->prepare_transfer_hardware(master);
  815. if (ret) {
  816. dev_err(&master->dev,
  817. "failed to prepare transfer hardware\n");
  818. if (master->auto_runtime_pm)
  819. pm_runtime_put(master->dev.parent);
  820. return;
  821. }
  822. }
  823. trace_spi_message_start(master->cur_msg);
  824. if (master->prepare_message) {
  825. ret = master->prepare_message(master, master->cur_msg);
  826. if (ret) {
  827. dev_err(&master->dev,
  828. "failed to prepare message: %d\n", ret);
  829. master->cur_msg->status = ret;
  830. spi_finalize_current_message(master);
  831. return;
  832. }
  833. master->cur_msg_prepared = true;
  834. }
  835. ret = spi_map_msg(master, master->cur_msg);
  836. if (ret) {
  837. master->cur_msg->status = ret;
  838. spi_finalize_current_message(master);
  839. return;
  840. }
  841. ret = master->transfer_one_message(master, master->cur_msg);
  842. if (ret) {
  843. dev_err(&master->dev,
  844. "failed to transfer one message from queue\n");
  845. return;
  846. }
  847. }
  848. /**
  849. * spi_pump_messages - kthread work function which processes spi message queue
  850. * @work: pointer to kthread work struct contained in the master struct
  851. */
  852. static void spi_pump_messages(struct kthread_work *work)
  853. {
  854. struct spi_master *master =
  855. container_of(work, struct spi_master, pump_messages);
  856. __spi_pump_messages(master, true);
  857. }
  858. static int spi_init_queue(struct spi_master *master)
  859. {
  860. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  861. master->running = false;
  862. master->busy = false;
  863. init_kthread_worker(&master->kworker);
  864. master->kworker_task = kthread_run(kthread_worker_fn,
  865. &master->kworker, "%s",
  866. dev_name(&master->dev));
  867. if (IS_ERR(master->kworker_task)) {
  868. dev_err(&master->dev, "failed to create message pump task\n");
  869. return PTR_ERR(master->kworker_task);
  870. }
  871. init_kthread_work(&master->pump_messages, spi_pump_messages);
  872. /*
  873. * Master config will indicate if this controller should run the
  874. * message pump with high (realtime) priority to reduce the transfer
  875. * latency on the bus by minimising the delay between a transfer
  876. * request and the scheduling of the message pump thread. Without this
  877. * setting the message pump thread will remain at default priority.
  878. */
  879. if (master->rt) {
  880. dev_info(&master->dev,
  881. "will run message pump with realtime priority\n");
  882. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  883. }
  884. return 0;
  885. }
  886. /**
  887. * spi_get_next_queued_message() - called by driver to check for queued
  888. * messages
  889. * @master: the master to check for queued messages
  890. *
  891. * If there are more messages in the queue, the next message is returned from
  892. * this call.
  893. */
  894. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  895. {
  896. struct spi_message *next;
  897. unsigned long flags;
  898. /* get a pointer to the next message, if any */
  899. spin_lock_irqsave(&master->queue_lock, flags);
  900. next = list_first_entry_or_null(&master->queue, struct spi_message,
  901. queue);
  902. spin_unlock_irqrestore(&master->queue_lock, flags);
  903. return next;
  904. }
  905. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  906. /**
  907. * spi_finalize_current_message() - the current message is complete
  908. * @master: the master to return the message to
  909. *
  910. * Called by the driver to notify the core that the message in the front of the
  911. * queue is complete and can be removed from the queue.
  912. */
  913. void spi_finalize_current_message(struct spi_master *master)
  914. {
  915. struct spi_message *mesg;
  916. unsigned long flags;
  917. int ret;
  918. spin_lock_irqsave(&master->queue_lock, flags);
  919. mesg = master->cur_msg;
  920. master->cur_msg = NULL;
  921. queue_kthread_work(&master->kworker, &master->pump_messages);
  922. spin_unlock_irqrestore(&master->queue_lock, flags);
  923. spi_unmap_msg(master, mesg);
  924. if (master->cur_msg_prepared && master->unprepare_message) {
  925. ret = master->unprepare_message(master, mesg);
  926. if (ret) {
  927. dev_err(&master->dev,
  928. "failed to unprepare message: %d\n", ret);
  929. }
  930. }
  931. trace_spi_message_done(mesg);
  932. master->cur_msg_prepared = false;
  933. mesg->state = NULL;
  934. if (mesg->complete)
  935. mesg->complete(mesg->context);
  936. }
  937. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  938. static int spi_start_queue(struct spi_master *master)
  939. {
  940. unsigned long flags;
  941. spin_lock_irqsave(&master->queue_lock, flags);
  942. if (master->running || master->busy) {
  943. spin_unlock_irqrestore(&master->queue_lock, flags);
  944. return -EBUSY;
  945. }
  946. master->running = true;
  947. master->cur_msg = NULL;
  948. spin_unlock_irqrestore(&master->queue_lock, flags);
  949. queue_kthread_work(&master->kworker, &master->pump_messages);
  950. return 0;
  951. }
  952. static int spi_stop_queue(struct spi_master *master)
  953. {
  954. unsigned long flags;
  955. unsigned limit = 500;
  956. int ret = 0;
  957. spin_lock_irqsave(&master->queue_lock, flags);
  958. /*
  959. * This is a bit lame, but is optimized for the common execution path.
  960. * A wait_queue on the master->busy could be used, but then the common
  961. * execution path (pump_messages) would be required to call wake_up or
  962. * friends on every SPI message. Do this instead.
  963. */
  964. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  965. spin_unlock_irqrestore(&master->queue_lock, flags);
  966. usleep_range(10000, 11000);
  967. spin_lock_irqsave(&master->queue_lock, flags);
  968. }
  969. if (!list_empty(&master->queue) || master->busy)
  970. ret = -EBUSY;
  971. else
  972. master->running = false;
  973. spin_unlock_irqrestore(&master->queue_lock, flags);
  974. if (ret) {
  975. dev_warn(&master->dev,
  976. "could not stop message queue\n");
  977. return ret;
  978. }
  979. return ret;
  980. }
  981. static int spi_destroy_queue(struct spi_master *master)
  982. {
  983. int ret;
  984. ret = spi_stop_queue(master);
  985. /*
  986. * flush_kthread_worker will block until all work is done.
  987. * If the reason that stop_queue timed out is that the work will never
  988. * finish, then it does no good to call flush/stop thread, so
  989. * return anyway.
  990. */
  991. if (ret) {
  992. dev_err(&master->dev, "problem destroying queue\n");
  993. return ret;
  994. }
  995. flush_kthread_worker(&master->kworker);
  996. kthread_stop(master->kworker_task);
  997. return 0;
  998. }
  999. static int __spi_queued_transfer(struct spi_device *spi,
  1000. struct spi_message *msg,
  1001. bool need_pump)
  1002. {
  1003. struct spi_master *master = spi->master;
  1004. unsigned long flags;
  1005. spin_lock_irqsave(&master->queue_lock, flags);
  1006. if (!master->running) {
  1007. spin_unlock_irqrestore(&master->queue_lock, flags);
  1008. return -ESHUTDOWN;
  1009. }
  1010. msg->actual_length = 0;
  1011. msg->status = -EINPROGRESS;
  1012. list_add_tail(&msg->queue, &master->queue);
  1013. if (!master->busy && need_pump)
  1014. queue_kthread_work(&master->kworker, &master->pump_messages);
  1015. spin_unlock_irqrestore(&master->queue_lock, flags);
  1016. return 0;
  1017. }
  1018. /**
  1019. * spi_queued_transfer - transfer function for queued transfers
  1020. * @spi: spi device which is requesting transfer
  1021. * @msg: spi message which is to handled is queued to driver queue
  1022. */
  1023. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  1024. {
  1025. return __spi_queued_transfer(spi, msg, true);
  1026. }
  1027. static int spi_master_initialize_queue(struct spi_master *master)
  1028. {
  1029. int ret;
  1030. master->transfer = spi_queued_transfer;
  1031. if (!master->transfer_one_message)
  1032. master->transfer_one_message = spi_transfer_one_message;
  1033. /* Initialize and start queue */
  1034. ret = spi_init_queue(master);
  1035. if (ret) {
  1036. dev_err(&master->dev, "problem initializing queue\n");
  1037. goto err_init_queue;
  1038. }
  1039. master->queued = true;
  1040. ret = spi_start_queue(master);
  1041. if (ret) {
  1042. dev_err(&master->dev, "problem starting queue\n");
  1043. goto err_start_queue;
  1044. }
  1045. return 0;
  1046. err_start_queue:
  1047. spi_destroy_queue(master);
  1048. err_init_queue:
  1049. return ret;
  1050. }
  1051. /*-------------------------------------------------------------------------*/
  1052. #if defined(CONFIG_OF)
  1053. static struct spi_device *
  1054. of_register_spi_device(struct spi_master *master, struct device_node *nc)
  1055. {
  1056. struct spi_device *spi;
  1057. int rc;
  1058. u32 value;
  1059. /* Alloc an spi_device */
  1060. spi = spi_alloc_device(master);
  1061. if (!spi) {
  1062. dev_err(&master->dev, "spi_device alloc error for %s\n",
  1063. nc->full_name);
  1064. rc = -ENOMEM;
  1065. goto err_out;
  1066. }
  1067. /* Select device driver */
  1068. rc = of_modalias_node(nc, spi->modalias,
  1069. sizeof(spi->modalias));
  1070. if (rc < 0) {
  1071. dev_err(&master->dev, "cannot find modalias for %s\n",
  1072. nc->full_name);
  1073. goto err_out;
  1074. }
  1075. /* Device address */
  1076. rc = of_property_read_u32(nc, "reg", &value);
  1077. if (rc) {
  1078. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  1079. nc->full_name, rc);
  1080. goto err_out;
  1081. }
  1082. spi->chip_select = value;
  1083. /* Mode (clock phase/polarity/etc.) */
  1084. if (of_find_property(nc, "spi-cpha", NULL))
  1085. spi->mode |= SPI_CPHA;
  1086. if (of_find_property(nc, "spi-cpol", NULL))
  1087. spi->mode |= SPI_CPOL;
  1088. if (of_find_property(nc, "spi-cs-high", NULL))
  1089. spi->mode |= SPI_CS_HIGH;
  1090. if (of_find_property(nc, "spi-3wire", NULL))
  1091. spi->mode |= SPI_3WIRE;
  1092. if (of_find_property(nc, "spi-lsb-first", NULL))
  1093. spi->mode |= SPI_LSB_FIRST;
  1094. /* Device DUAL/QUAD mode */
  1095. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  1096. switch (value) {
  1097. case 1:
  1098. break;
  1099. case 2:
  1100. spi->mode |= SPI_TX_DUAL;
  1101. break;
  1102. case 4:
  1103. spi->mode |= SPI_TX_QUAD;
  1104. break;
  1105. default:
  1106. dev_warn(&master->dev,
  1107. "spi-tx-bus-width %d not supported\n",
  1108. value);
  1109. break;
  1110. }
  1111. }
  1112. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  1113. switch (value) {
  1114. case 1:
  1115. break;
  1116. case 2:
  1117. spi->mode |= SPI_RX_DUAL;
  1118. break;
  1119. case 4:
  1120. spi->mode |= SPI_RX_QUAD;
  1121. break;
  1122. default:
  1123. dev_warn(&master->dev,
  1124. "spi-rx-bus-width %d not supported\n",
  1125. value);
  1126. break;
  1127. }
  1128. }
  1129. /* Device speed */
  1130. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  1131. if (rc) {
  1132. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  1133. nc->full_name, rc);
  1134. goto err_out;
  1135. }
  1136. spi->max_speed_hz = value;
  1137. /* IRQ */
  1138. spi->irq = irq_of_parse_and_map(nc, 0);
  1139. /* Store a pointer to the node in the device structure */
  1140. of_node_get(nc);
  1141. spi->dev.of_node = nc;
  1142. /* Register the new device */
  1143. rc = spi_add_device(spi);
  1144. if (rc) {
  1145. dev_err(&master->dev, "spi_device register error %s\n",
  1146. nc->full_name);
  1147. goto err_out;
  1148. }
  1149. return spi;
  1150. err_out:
  1151. spi_dev_put(spi);
  1152. return ERR_PTR(rc);
  1153. }
  1154. /**
  1155. * of_register_spi_devices() - Register child devices onto the SPI bus
  1156. * @master: Pointer to spi_master device
  1157. *
  1158. * Registers an spi_device for each child node of master node which has a 'reg'
  1159. * property.
  1160. */
  1161. static void of_register_spi_devices(struct spi_master *master)
  1162. {
  1163. struct spi_device *spi;
  1164. struct device_node *nc;
  1165. if (!master->dev.of_node)
  1166. return;
  1167. for_each_available_child_of_node(master->dev.of_node, nc) {
  1168. spi = of_register_spi_device(master, nc);
  1169. if (IS_ERR(spi))
  1170. dev_warn(&master->dev, "Failed to create SPI device for %s\n",
  1171. nc->full_name);
  1172. }
  1173. }
  1174. #else
  1175. static void of_register_spi_devices(struct spi_master *master) { }
  1176. #endif
  1177. #ifdef CONFIG_ACPI
  1178. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  1179. {
  1180. struct spi_device *spi = data;
  1181. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  1182. struct acpi_resource_spi_serialbus *sb;
  1183. sb = &ares->data.spi_serial_bus;
  1184. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  1185. spi->chip_select = sb->device_selection;
  1186. spi->max_speed_hz = sb->connection_speed;
  1187. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  1188. spi->mode |= SPI_CPHA;
  1189. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  1190. spi->mode |= SPI_CPOL;
  1191. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  1192. spi->mode |= SPI_CS_HIGH;
  1193. }
  1194. } else if (spi->irq < 0) {
  1195. struct resource r;
  1196. if (acpi_dev_resource_interrupt(ares, 0, &r))
  1197. spi->irq = r.start;
  1198. }
  1199. /* Always tell the ACPI core to skip this resource */
  1200. return 1;
  1201. }
  1202. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  1203. void *data, void **return_value)
  1204. {
  1205. struct spi_master *master = data;
  1206. struct list_head resource_list;
  1207. struct acpi_device *adev;
  1208. struct spi_device *spi;
  1209. int ret;
  1210. if (acpi_bus_get_device(handle, &adev))
  1211. return AE_OK;
  1212. if (acpi_bus_get_status(adev) || !adev->status.present)
  1213. return AE_OK;
  1214. spi = spi_alloc_device(master);
  1215. if (!spi) {
  1216. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  1217. dev_name(&adev->dev));
  1218. return AE_NO_MEMORY;
  1219. }
  1220. ACPI_COMPANION_SET(&spi->dev, adev);
  1221. spi->irq = -1;
  1222. INIT_LIST_HEAD(&resource_list);
  1223. ret = acpi_dev_get_resources(adev, &resource_list,
  1224. acpi_spi_add_resource, spi);
  1225. acpi_dev_free_resource_list(&resource_list);
  1226. if (ret < 0 || !spi->max_speed_hz) {
  1227. spi_dev_put(spi);
  1228. return AE_OK;
  1229. }
  1230. adev->power.flags.ignore_parent = true;
  1231. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  1232. if (spi_add_device(spi)) {
  1233. adev->power.flags.ignore_parent = false;
  1234. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  1235. dev_name(&adev->dev));
  1236. spi_dev_put(spi);
  1237. }
  1238. return AE_OK;
  1239. }
  1240. static void acpi_register_spi_devices(struct spi_master *master)
  1241. {
  1242. acpi_status status;
  1243. acpi_handle handle;
  1244. handle = ACPI_HANDLE(master->dev.parent);
  1245. if (!handle)
  1246. return;
  1247. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1248. acpi_spi_add_device, NULL,
  1249. master, NULL);
  1250. if (ACPI_FAILURE(status))
  1251. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1252. }
  1253. #else
  1254. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1255. #endif /* CONFIG_ACPI */
  1256. static void spi_master_release(struct device *dev)
  1257. {
  1258. struct spi_master *master;
  1259. master = container_of(dev, struct spi_master, dev);
  1260. kfree(master);
  1261. }
  1262. static struct class spi_master_class = {
  1263. .name = "spi_master",
  1264. .owner = THIS_MODULE,
  1265. .dev_release = spi_master_release,
  1266. };
  1267. /**
  1268. * spi_alloc_master - allocate SPI master controller
  1269. * @dev: the controller, possibly using the platform_bus
  1270. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1271. * memory is in the driver_data field of the returned device,
  1272. * accessible with spi_master_get_devdata().
  1273. * Context: can sleep
  1274. *
  1275. * This call is used only by SPI master controller drivers, which are the
  1276. * only ones directly touching chip registers. It's how they allocate
  1277. * an spi_master structure, prior to calling spi_register_master().
  1278. *
  1279. * This must be called from context that can sleep. It returns the SPI
  1280. * master structure on success, else NULL.
  1281. *
  1282. * The caller is responsible for assigning the bus number and initializing
  1283. * the master's methods before calling spi_register_master(); and (after errors
  1284. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  1285. * leak.
  1286. */
  1287. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1288. {
  1289. struct spi_master *master;
  1290. if (!dev)
  1291. return NULL;
  1292. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1293. if (!master)
  1294. return NULL;
  1295. device_initialize(&master->dev);
  1296. master->bus_num = -1;
  1297. master->num_chipselect = 1;
  1298. master->dev.class = &spi_master_class;
  1299. master->dev.parent = get_device(dev);
  1300. spi_master_set_devdata(master, &master[1]);
  1301. return master;
  1302. }
  1303. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1304. #ifdef CONFIG_OF
  1305. static int of_spi_register_master(struct spi_master *master)
  1306. {
  1307. int nb, i, *cs;
  1308. struct device_node *np = master->dev.of_node;
  1309. if (!np)
  1310. return 0;
  1311. nb = of_gpio_named_count(np, "cs-gpios");
  1312. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1313. /* Return error only for an incorrectly formed cs-gpios property */
  1314. if (nb == 0 || nb == -ENOENT)
  1315. return 0;
  1316. else if (nb < 0)
  1317. return nb;
  1318. cs = devm_kzalloc(&master->dev,
  1319. sizeof(int) * master->num_chipselect,
  1320. GFP_KERNEL);
  1321. master->cs_gpios = cs;
  1322. if (!master->cs_gpios)
  1323. return -ENOMEM;
  1324. for (i = 0; i < master->num_chipselect; i++)
  1325. cs[i] = -ENOENT;
  1326. for (i = 0; i < nb; i++)
  1327. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1328. return 0;
  1329. }
  1330. #else
  1331. static int of_spi_register_master(struct spi_master *master)
  1332. {
  1333. return 0;
  1334. }
  1335. #endif
  1336. /**
  1337. * spi_register_master - register SPI master controller
  1338. * @master: initialized master, originally from spi_alloc_master()
  1339. * Context: can sleep
  1340. *
  1341. * SPI master controllers connect to their drivers using some non-SPI bus,
  1342. * such as the platform bus. The final stage of probe() in that code
  1343. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1344. *
  1345. * SPI controllers use board specific (often SOC specific) bus numbers,
  1346. * and board-specific addressing for SPI devices combines those numbers
  1347. * with chip select numbers. Since SPI does not directly support dynamic
  1348. * device identification, boards need configuration tables telling which
  1349. * chip is at which address.
  1350. *
  1351. * This must be called from context that can sleep. It returns zero on
  1352. * success, else a negative error code (dropping the master's refcount).
  1353. * After a successful return, the caller is responsible for calling
  1354. * spi_unregister_master().
  1355. */
  1356. int spi_register_master(struct spi_master *master)
  1357. {
  1358. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1359. struct device *dev = master->dev.parent;
  1360. struct boardinfo *bi;
  1361. int status = -ENODEV;
  1362. int dynamic = 0;
  1363. if (!dev)
  1364. return -ENODEV;
  1365. status = of_spi_register_master(master);
  1366. if (status)
  1367. return status;
  1368. /* even if it's just one always-selected device, there must
  1369. * be at least one chipselect
  1370. */
  1371. if (master->num_chipselect == 0)
  1372. return -EINVAL;
  1373. if ((master->bus_num < 0) && master->dev.of_node)
  1374. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1375. /* convention: dynamically assigned bus IDs count down from the max */
  1376. if (master->bus_num < 0) {
  1377. /* FIXME switch to an IDR based scheme, something like
  1378. * I2C now uses, so we can't run out of "dynamic" IDs
  1379. */
  1380. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1381. dynamic = 1;
  1382. }
  1383. INIT_LIST_HEAD(&master->queue);
  1384. spin_lock_init(&master->queue_lock);
  1385. spin_lock_init(&master->bus_lock_spinlock);
  1386. mutex_init(&master->bus_lock_mutex);
  1387. master->bus_lock_flag = 0;
  1388. init_completion(&master->xfer_completion);
  1389. if (!master->max_dma_len)
  1390. master->max_dma_len = INT_MAX;
  1391. /* register the device, then userspace will see it.
  1392. * registration fails if the bus ID is in use.
  1393. */
  1394. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1395. status = device_add(&master->dev);
  1396. if (status < 0)
  1397. goto done;
  1398. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1399. dynamic ? " (dynamic)" : "");
  1400. /* If we're using a queued driver, start the queue */
  1401. if (master->transfer)
  1402. dev_info(dev, "master is unqueued, this is deprecated\n");
  1403. else {
  1404. status = spi_master_initialize_queue(master);
  1405. if (status) {
  1406. device_del(&master->dev);
  1407. goto done;
  1408. }
  1409. }
  1410. mutex_lock(&board_lock);
  1411. list_add_tail(&master->list, &spi_master_list);
  1412. list_for_each_entry(bi, &board_list, list)
  1413. spi_match_master_to_boardinfo(master, &bi->board_info);
  1414. mutex_unlock(&board_lock);
  1415. /* Register devices from the device tree and ACPI */
  1416. of_register_spi_devices(master);
  1417. acpi_register_spi_devices(master);
  1418. done:
  1419. return status;
  1420. }
  1421. EXPORT_SYMBOL_GPL(spi_register_master);
  1422. static void devm_spi_unregister(struct device *dev, void *res)
  1423. {
  1424. spi_unregister_master(*(struct spi_master **)res);
  1425. }
  1426. /**
  1427. * dev_spi_register_master - register managed SPI master controller
  1428. * @dev: device managing SPI master
  1429. * @master: initialized master, originally from spi_alloc_master()
  1430. * Context: can sleep
  1431. *
  1432. * Register a SPI device as with spi_register_master() which will
  1433. * automatically be unregister
  1434. */
  1435. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1436. {
  1437. struct spi_master **ptr;
  1438. int ret;
  1439. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1440. if (!ptr)
  1441. return -ENOMEM;
  1442. ret = spi_register_master(master);
  1443. if (!ret) {
  1444. *ptr = master;
  1445. devres_add(dev, ptr);
  1446. } else {
  1447. devres_free(ptr);
  1448. }
  1449. return ret;
  1450. }
  1451. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1452. static int __unregister(struct device *dev, void *null)
  1453. {
  1454. spi_unregister_device(to_spi_device(dev));
  1455. return 0;
  1456. }
  1457. /**
  1458. * spi_unregister_master - unregister SPI master controller
  1459. * @master: the master being unregistered
  1460. * Context: can sleep
  1461. *
  1462. * This call is used only by SPI master controller drivers, which are the
  1463. * only ones directly touching chip registers.
  1464. *
  1465. * This must be called from context that can sleep.
  1466. */
  1467. void spi_unregister_master(struct spi_master *master)
  1468. {
  1469. int dummy;
  1470. if (master->queued) {
  1471. if (spi_destroy_queue(master))
  1472. dev_err(&master->dev, "queue remove failed\n");
  1473. }
  1474. mutex_lock(&board_lock);
  1475. list_del(&master->list);
  1476. mutex_unlock(&board_lock);
  1477. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1478. device_unregister(&master->dev);
  1479. }
  1480. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1481. int spi_master_suspend(struct spi_master *master)
  1482. {
  1483. int ret;
  1484. /* Basically no-ops for non-queued masters */
  1485. if (!master->queued)
  1486. return 0;
  1487. ret = spi_stop_queue(master);
  1488. if (ret)
  1489. dev_err(&master->dev, "queue stop failed\n");
  1490. return ret;
  1491. }
  1492. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1493. int spi_master_resume(struct spi_master *master)
  1494. {
  1495. int ret;
  1496. if (!master->queued)
  1497. return 0;
  1498. ret = spi_start_queue(master);
  1499. if (ret)
  1500. dev_err(&master->dev, "queue restart failed\n");
  1501. return ret;
  1502. }
  1503. EXPORT_SYMBOL_GPL(spi_master_resume);
  1504. static int __spi_master_match(struct device *dev, const void *data)
  1505. {
  1506. struct spi_master *m;
  1507. const u16 *bus_num = data;
  1508. m = container_of(dev, struct spi_master, dev);
  1509. return m->bus_num == *bus_num;
  1510. }
  1511. /**
  1512. * spi_busnum_to_master - look up master associated with bus_num
  1513. * @bus_num: the master's bus number
  1514. * Context: can sleep
  1515. *
  1516. * This call may be used with devices that are registered after
  1517. * arch init time. It returns a refcounted pointer to the relevant
  1518. * spi_master (which the caller must release), or NULL if there is
  1519. * no such master registered.
  1520. */
  1521. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1522. {
  1523. struct device *dev;
  1524. struct spi_master *master = NULL;
  1525. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1526. __spi_master_match);
  1527. if (dev)
  1528. master = container_of(dev, struct spi_master, dev);
  1529. /* reference got in class_find_device */
  1530. return master;
  1531. }
  1532. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1533. /*-------------------------------------------------------------------------*/
  1534. /* Core methods for SPI master protocol drivers. Some of the
  1535. * other core methods are currently defined as inline functions.
  1536. */
  1537. /**
  1538. * spi_setup - setup SPI mode and clock rate
  1539. * @spi: the device whose settings are being modified
  1540. * Context: can sleep, and no requests are queued to the device
  1541. *
  1542. * SPI protocol drivers may need to update the transfer mode if the
  1543. * device doesn't work with its default. They may likewise need
  1544. * to update clock rates or word sizes from initial values. This function
  1545. * changes those settings, and must be called from a context that can sleep.
  1546. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1547. * effect the next time the device is selected and data is transferred to
  1548. * or from it. When this function returns, the spi device is deselected.
  1549. *
  1550. * Note that this call will fail if the protocol driver specifies an option
  1551. * that the underlying controller or its driver does not support. For
  1552. * example, not all hardware supports wire transfers using nine bit words,
  1553. * LSB-first wire encoding, or active-high chipselects.
  1554. */
  1555. int spi_setup(struct spi_device *spi)
  1556. {
  1557. unsigned bad_bits, ugly_bits;
  1558. int status = 0;
  1559. /* check mode to prevent that DUAL and QUAD set at the same time
  1560. */
  1561. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  1562. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  1563. dev_err(&spi->dev,
  1564. "setup: can not select dual and quad at the same time\n");
  1565. return -EINVAL;
  1566. }
  1567. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  1568. */
  1569. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  1570. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  1571. return -EINVAL;
  1572. /* help drivers fail *cleanly* when they need options
  1573. * that aren't supported with their current master
  1574. */
  1575. bad_bits = spi->mode & ~spi->master->mode_bits;
  1576. ugly_bits = bad_bits &
  1577. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
  1578. if (ugly_bits) {
  1579. dev_warn(&spi->dev,
  1580. "setup: ignoring unsupported mode bits %x\n",
  1581. ugly_bits);
  1582. spi->mode &= ~ugly_bits;
  1583. bad_bits &= ~ugly_bits;
  1584. }
  1585. if (bad_bits) {
  1586. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1587. bad_bits);
  1588. return -EINVAL;
  1589. }
  1590. if (!spi->bits_per_word)
  1591. spi->bits_per_word = 8;
  1592. if (!spi->max_speed_hz)
  1593. spi->max_speed_hz = spi->master->max_speed_hz;
  1594. spi_set_cs(spi, false);
  1595. if (spi->master->setup)
  1596. status = spi->master->setup(spi);
  1597. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  1598. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1599. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1600. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1601. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1602. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1603. spi->bits_per_word, spi->max_speed_hz,
  1604. status);
  1605. return status;
  1606. }
  1607. EXPORT_SYMBOL_GPL(spi_setup);
  1608. static int __spi_validate(struct spi_device *spi, struct spi_message *message)
  1609. {
  1610. struct spi_master *master = spi->master;
  1611. struct spi_transfer *xfer;
  1612. int w_size;
  1613. if (list_empty(&message->transfers))
  1614. return -EINVAL;
  1615. /* Half-duplex links include original MicroWire, and ones with
  1616. * only one data pin like SPI_3WIRE (switches direction) or where
  1617. * either MOSI or MISO is missing. They can also be caused by
  1618. * software limitations.
  1619. */
  1620. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1621. || (spi->mode & SPI_3WIRE)) {
  1622. unsigned flags = master->flags;
  1623. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1624. if (xfer->rx_buf && xfer->tx_buf)
  1625. return -EINVAL;
  1626. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1627. return -EINVAL;
  1628. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1629. return -EINVAL;
  1630. }
  1631. }
  1632. /**
  1633. * Set transfer bits_per_word and max speed as spi device default if
  1634. * it is not set for this transfer.
  1635. * Set transfer tx_nbits and rx_nbits as single transfer default
  1636. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  1637. */
  1638. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1639. message->frame_length += xfer->len;
  1640. if (!xfer->bits_per_word)
  1641. xfer->bits_per_word = spi->bits_per_word;
  1642. if (!xfer->speed_hz)
  1643. xfer->speed_hz = spi->max_speed_hz;
  1644. if (master->max_speed_hz &&
  1645. xfer->speed_hz > master->max_speed_hz)
  1646. xfer->speed_hz = master->max_speed_hz;
  1647. if (master->bits_per_word_mask) {
  1648. /* Only 32 bits fit in the mask */
  1649. if (xfer->bits_per_word > 32)
  1650. return -EINVAL;
  1651. if (!(master->bits_per_word_mask &
  1652. BIT(xfer->bits_per_word - 1)))
  1653. return -EINVAL;
  1654. }
  1655. /*
  1656. * SPI transfer length should be multiple of SPI word size
  1657. * where SPI word size should be power-of-two multiple
  1658. */
  1659. if (xfer->bits_per_word <= 8)
  1660. w_size = 1;
  1661. else if (xfer->bits_per_word <= 16)
  1662. w_size = 2;
  1663. else
  1664. w_size = 4;
  1665. /* No partial transfers accepted */
  1666. if (xfer->len % w_size)
  1667. return -EINVAL;
  1668. if (xfer->speed_hz && master->min_speed_hz &&
  1669. xfer->speed_hz < master->min_speed_hz)
  1670. return -EINVAL;
  1671. if (xfer->tx_buf && !xfer->tx_nbits)
  1672. xfer->tx_nbits = SPI_NBITS_SINGLE;
  1673. if (xfer->rx_buf && !xfer->rx_nbits)
  1674. xfer->rx_nbits = SPI_NBITS_SINGLE;
  1675. /* check transfer tx/rx_nbits:
  1676. * 1. check the value matches one of single, dual and quad
  1677. * 2. check tx/rx_nbits match the mode in spi_device
  1678. */
  1679. if (xfer->tx_buf) {
  1680. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  1681. xfer->tx_nbits != SPI_NBITS_DUAL &&
  1682. xfer->tx_nbits != SPI_NBITS_QUAD)
  1683. return -EINVAL;
  1684. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  1685. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  1686. return -EINVAL;
  1687. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  1688. !(spi->mode & SPI_TX_QUAD))
  1689. return -EINVAL;
  1690. }
  1691. /* check transfer rx_nbits */
  1692. if (xfer->rx_buf) {
  1693. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  1694. xfer->rx_nbits != SPI_NBITS_DUAL &&
  1695. xfer->rx_nbits != SPI_NBITS_QUAD)
  1696. return -EINVAL;
  1697. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  1698. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  1699. return -EINVAL;
  1700. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  1701. !(spi->mode & SPI_RX_QUAD))
  1702. return -EINVAL;
  1703. }
  1704. }
  1705. message->status = -EINPROGRESS;
  1706. return 0;
  1707. }
  1708. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1709. {
  1710. struct spi_master *master = spi->master;
  1711. message->spi = spi;
  1712. trace_spi_message_submit(message);
  1713. return master->transfer(spi, message);
  1714. }
  1715. /**
  1716. * spi_async - asynchronous SPI transfer
  1717. * @spi: device with which data will be exchanged
  1718. * @message: describes the data transfers, including completion callback
  1719. * Context: any (irqs may be blocked, etc)
  1720. *
  1721. * This call may be used in_irq and other contexts which can't sleep,
  1722. * as well as from task contexts which can sleep.
  1723. *
  1724. * The completion callback is invoked in a context which can't sleep.
  1725. * Before that invocation, the value of message->status is undefined.
  1726. * When the callback is issued, message->status holds either zero (to
  1727. * indicate complete success) or a negative error code. After that
  1728. * callback returns, the driver which issued the transfer request may
  1729. * deallocate the associated memory; it's no longer in use by any SPI
  1730. * core or controller driver code.
  1731. *
  1732. * Note that although all messages to a spi_device are handled in
  1733. * FIFO order, messages may go to different devices in other orders.
  1734. * Some device might be higher priority, or have various "hard" access
  1735. * time requirements, for example.
  1736. *
  1737. * On detection of any fault during the transfer, processing of
  1738. * the entire message is aborted, and the device is deselected.
  1739. * Until returning from the associated message completion callback,
  1740. * no other spi_message queued to that device will be processed.
  1741. * (This rule applies equally to all the synchronous transfer calls,
  1742. * which are wrappers around this core asynchronous primitive.)
  1743. */
  1744. int spi_async(struct spi_device *spi, struct spi_message *message)
  1745. {
  1746. struct spi_master *master = spi->master;
  1747. int ret;
  1748. unsigned long flags;
  1749. ret = __spi_validate(spi, message);
  1750. if (ret != 0)
  1751. return ret;
  1752. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1753. if (master->bus_lock_flag)
  1754. ret = -EBUSY;
  1755. else
  1756. ret = __spi_async(spi, message);
  1757. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1758. return ret;
  1759. }
  1760. EXPORT_SYMBOL_GPL(spi_async);
  1761. /**
  1762. * spi_async_locked - version of spi_async with exclusive bus usage
  1763. * @spi: device with which data will be exchanged
  1764. * @message: describes the data transfers, including completion callback
  1765. * Context: any (irqs may be blocked, etc)
  1766. *
  1767. * This call may be used in_irq and other contexts which can't sleep,
  1768. * as well as from task contexts which can sleep.
  1769. *
  1770. * The completion callback is invoked in a context which can't sleep.
  1771. * Before that invocation, the value of message->status is undefined.
  1772. * When the callback is issued, message->status holds either zero (to
  1773. * indicate complete success) or a negative error code. After that
  1774. * callback returns, the driver which issued the transfer request may
  1775. * deallocate the associated memory; it's no longer in use by any SPI
  1776. * core or controller driver code.
  1777. *
  1778. * Note that although all messages to a spi_device are handled in
  1779. * FIFO order, messages may go to different devices in other orders.
  1780. * Some device might be higher priority, or have various "hard" access
  1781. * time requirements, for example.
  1782. *
  1783. * On detection of any fault during the transfer, processing of
  1784. * the entire message is aborted, and the device is deselected.
  1785. * Until returning from the associated message completion callback,
  1786. * no other spi_message queued to that device will be processed.
  1787. * (This rule applies equally to all the synchronous transfer calls,
  1788. * which are wrappers around this core asynchronous primitive.)
  1789. */
  1790. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1791. {
  1792. struct spi_master *master = spi->master;
  1793. int ret;
  1794. unsigned long flags;
  1795. ret = __spi_validate(spi, message);
  1796. if (ret != 0)
  1797. return ret;
  1798. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1799. ret = __spi_async(spi, message);
  1800. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1801. return ret;
  1802. }
  1803. EXPORT_SYMBOL_GPL(spi_async_locked);
  1804. /*-------------------------------------------------------------------------*/
  1805. /* Utility methods for SPI master protocol drivers, layered on
  1806. * top of the core. Some other utility methods are defined as
  1807. * inline functions.
  1808. */
  1809. static void spi_complete(void *arg)
  1810. {
  1811. complete(arg);
  1812. }
  1813. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1814. int bus_locked)
  1815. {
  1816. DECLARE_COMPLETION_ONSTACK(done);
  1817. int status;
  1818. struct spi_master *master = spi->master;
  1819. unsigned long flags;
  1820. status = __spi_validate(spi, message);
  1821. if (status != 0)
  1822. return status;
  1823. message->complete = spi_complete;
  1824. message->context = &done;
  1825. message->spi = spi;
  1826. if (!bus_locked)
  1827. mutex_lock(&master->bus_lock_mutex);
  1828. /* If we're not using the legacy transfer method then we will
  1829. * try to transfer in the calling context so special case.
  1830. * This code would be less tricky if we could remove the
  1831. * support for driver implemented message queues.
  1832. */
  1833. if (master->transfer == spi_queued_transfer) {
  1834. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1835. trace_spi_message_submit(message);
  1836. status = __spi_queued_transfer(spi, message, false);
  1837. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1838. } else {
  1839. status = spi_async_locked(spi, message);
  1840. }
  1841. if (!bus_locked)
  1842. mutex_unlock(&master->bus_lock_mutex);
  1843. if (status == 0) {
  1844. /* Push out the messages in the calling context if we
  1845. * can.
  1846. */
  1847. if (master->transfer == spi_queued_transfer)
  1848. __spi_pump_messages(master, false);
  1849. wait_for_completion(&done);
  1850. status = message->status;
  1851. }
  1852. message->context = NULL;
  1853. return status;
  1854. }
  1855. /**
  1856. * spi_sync - blocking/synchronous SPI data transfers
  1857. * @spi: device with which data will be exchanged
  1858. * @message: describes the data transfers
  1859. * Context: can sleep
  1860. *
  1861. * This call may only be used from a context that may sleep. The sleep
  1862. * is non-interruptible, and has no timeout. Low-overhead controller
  1863. * drivers may DMA directly into and out of the message buffers.
  1864. *
  1865. * Note that the SPI device's chip select is active during the message,
  1866. * and then is normally disabled between messages. Drivers for some
  1867. * frequently-used devices may want to minimize costs of selecting a chip,
  1868. * by leaving it selected in anticipation that the next message will go
  1869. * to the same chip. (That may increase power usage.)
  1870. *
  1871. * Also, the caller is guaranteeing that the memory associated with the
  1872. * message will not be freed before this call returns.
  1873. *
  1874. * It returns zero on success, else a negative error code.
  1875. */
  1876. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1877. {
  1878. return __spi_sync(spi, message, 0);
  1879. }
  1880. EXPORT_SYMBOL_GPL(spi_sync);
  1881. /**
  1882. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1883. * @spi: device with which data will be exchanged
  1884. * @message: describes the data transfers
  1885. * Context: can sleep
  1886. *
  1887. * This call may only be used from a context that may sleep. The sleep
  1888. * is non-interruptible, and has no timeout. Low-overhead controller
  1889. * drivers may DMA directly into and out of the message buffers.
  1890. *
  1891. * This call should be used by drivers that require exclusive access to the
  1892. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1893. * be released by a spi_bus_unlock call when the exclusive access is over.
  1894. *
  1895. * It returns zero on success, else a negative error code.
  1896. */
  1897. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1898. {
  1899. return __spi_sync(spi, message, 1);
  1900. }
  1901. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1902. /**
  1903. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1904. * @master: SPI bus master that should be locked for exclusive bus access
  1905. * Context: can sleep
  1906. *
  1907. * This call may only be used from a context that may sleep. The sleep
  1908. * is non-interruptible, and has no timeout.
  1909. *
  1910. * This call should be used by drivers that require exclusive access to the
  1911. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1912. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1913. * and spi_async_locked calls when the SPI bus lock is held.
  1914. *
  1915. * It returns zero on success, else a negative error code.
  1916. */
  1917. int spi_bus_lock(struct spi_master *master)
  1918. {
  1919. unsigned long flags;
  1920. mutex_lock(&master->bus_lock_mutex);
  1921. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1922. master->bus_lock_flag = 1;
  1923. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1924. /* mutex remains locked until spi_bus_unlock is called */
  1925. return 0;
  1926. }
  1927. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1928. /**
  1929. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1930. * @master: SPI bus master that was locked for exclusive bus access
  1931. * Context: can sleep
  1932. *
  1933. * This call may only be used from a context that may sleep. The sleep
  1934. * is non-interruptible, and has no timeout.
  1935. *
  1936. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1937. * call.
  1938. *
  1939. * It returns zero on success, else a negative error code.
  1940. */
  1941. int spi_bus_unlock(struct spi_master *master)
  1942. {
  1943. master->bus_lock_flag = 0;
  1944. mutex_unlock(&master->bus_lock_mutex);
  1945. return 0;
  1946. }
  1947. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1948. /* portable code must never pass more than 32 bytes */
  1949. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  1950. static u8 *buf;
  1951. /**
  1952. * spi_write_then_read - SPI synchronous write followed by read
  1953. * @spi: device with which data will be exchanged
  1954. * @txbuf: data to be written (need not be dma-safe)
  1955. * @n_tx: size of txbuf, in bytes
  1956. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1957. * @n_rx: size of rxbuf, in bytes
  1958. * Context: can sleep
  1959. *
  1960. * This performs a half duplex MicroWire style transaction with the
  1961. * device, sending txbuf and then reading rxbuf. The return value
  1962. * is zero for success, else a negative errno status code.
  1963. * This call may only be used from a context that may sleep.
  1964. *
  1965. * Parameters to this routine are always copied using a small buffer;
  1966. * portable code should never use this for more than 32 bytes.
  1967. * Performance-sensitive or bulk transfer code should instead use
  1968. * spi_{async,sync}() calls with dma-safe buffers.
  1969. */
  1970. int spi_write_then_read(struct spi_device *spi,
  1971. const void *txbuf, unsigned n_tx,
  1972. void *rxbuf, unsigned n_rx)
  1973. {
  1974. static DEFINE_MUTEX(lock);
  1975. int status;
  1976. struct spi_message message;
  1977. struct spi_transfer x[2];
  1978. u8 *local_buf;
  1979. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1980. * copying here, (as a pure convenience thing), but we can
  1981. * keep heap costs out of the hot path unless someone else is
  1982. * using the pre-allocated buffer or the transfer is too large.
  1983. */
  1984. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1985. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1986. GFP_KERNEL | GFP_DMA);
  1987. if (!local_buf)
  1988. return -ENOMEM;
  1989. } else {
  1990. local_buf = buf;
  1991. }
  1992. spi_message_init(&message);
  1993. memset(x, 0, sizeof(x));
  1994. if (n_tx) {
  1995. x[0].len = n_tx;
  1996. spi_message_add_tail(&x[0], &message);
  1997. }
  1998. if (n_rx) {
  1999. x[1].len = n_rx;
  2000. spi_message_add_tail(&x[1], &message);
  2001. }
  2002. memcpy(local_buf, txbuf, n_tx);
  2003. x[0].tx_buf = local_buf;
  2004. x[1].rx_buf = local_buf + n_tx;
  2005. /* do the i/o */
  2006. status = spi_sync(spi, &message);
  2007. if (status == 0)
  2008. memcpy(rxbuf, x[1].rx_buf, n_rx);
  2009. if (x[0].tx_buf == buf)
  2010. mutex_unlock(&lock);
  2011. else
  2012. kfree(local_buf);
  2013. return status;
  2014. }
  2015. EXPORT_SYMBOL_GPL(spi_write_then_read);
  2016. /*-------------------------------------------------------------------------*/
  2017. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  2018. static int __spi_of_device_match(struct device *dev, void *data)
  2019. {
  2020. return dev->of_node == data;
  2021. }
  2022. /* must call put_device() when done with returned spi_device device */
  2023. static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
  2024. {
  2025. struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
  2026. __spi_of_device_match);
  2027. return dev ? to_spi_device(dev) : NULL;
  2028. }
  2029. static int __spi_of_master_match(struct device *dev, const void *data)
  2030. {
  2031. return dev->of_node == data;
  2032. }
  2033. /* the spi masters are not using spi_bus, so we find it with another way */
  2034. static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
  2035. {
  2036. struct device *dev;
  2037. dev = class_find_device(&spi_master_class, NULL, node,
  2038. __spi_of_master_match);
  2039. if (!dev)
  2040. return NULL;
  2041. /* reference got in class_find_device */
  2042. return container_of(dev, struct spi_master, dev);
  2043. }
  2044. static int of_spi_notify(struct notifier_block *nb, unsigned long action,
  2045. void *arg)
  2046. {
  2047. struct of_reconfig_data *rd = arg;
  2048. struct spi_master *master;
  2049. struct spi_device *spi;
  2050. switch (of_reconfig_get_state_change(action, arg)) {
  2051. case OF_RECONFIG_CHANGE_ADD:
  2052. master = of_find_spi_master_by_node(rd->dn->parent);
  2053. if (master == NULL)
  2054. return NOTIFY_OK; /* not for us */
  2055. spi = of_register_spi_device(master, rd->dn);
  2056. put_device(&master->dev);
  2057. if (IS_ERR(spi)) {
  2058. pr_err("%s: failed to create for '%s'\n",
  2059. __func__, rd->dn->full_name);
  2060. return notifier_from_errno(PTR_ERR(spi));
  2061. }
  2062. break;
  2063. case OF_RECONFIG_CHANGE_REMOVE:
  2064. /* find our device by node */
  2065. spi = of_find_spi_device_by_node(rd->dn);
  2066. if (spi == NULL)
  2067. return NOTIFY_OK; /* no? not meant for us */
  2068. /* unregister takes one ref away */
  2069. spi_unregister_device(spi);
  2070. /* and put the reference of the find */
  2071. put_device(&spi->dev);
  2072. break;
  2073. }
  2074. return NOTIFY_OK;
  2075. }
  2076. static struct notifier_block spi_of_notifier = {
  2077. .notifier_call = of_spi_notify,
  2078. };
  2079. #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2080. extern struct notifier_block spi_of_notifier;
  2081. #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2082. static int __init spi_init(void)
  2083. {
  2084. int status;
  2085. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  2086. if (!buf) {
  2087. status = -ENOMEM;
  2088. goto err0;
  2089. }
  2090. status = bus_register(&spi_bus_type);
  2091. if (status < 0)
  2092. goto err1;
  2093. status = class_register(&spi_master_class);
  2094. if (status < 0)
  2095. goto err2;
  2096. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  2097. WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
  2098. return 0;
  2099. err2:
  2100. bus_unregister(&spi_bus_type);
  2101. err1:
  2102. kfree(buf);
  2103. buf = NULL;
  2104. err0:
  2105. return status;
  2106. }
  2107. /* board_info is normally registered in arch_initcall(),
  2108. * but even essential drivers wait till later
  2109. *
  2110. * REVISIT only boardinfo really needs static linking. the rest (device and
  2111. * driver registration) _could_ be dynamically linked (modular) ... costs
  2112. * include needing to have boardinfo data structures be much more public.
  2113. */
  2114. postcore_initcall(spi_init);