spi.c 92 KB

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