spi.c 69 KB

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