spi.c 85 KB

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