spi.c 75 KB

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