spi.c 88 KB

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