spi.c 63 KB

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