spi.c 95 KB

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