spi.c 94 KB

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