spi.c 98 KB

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