spi.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291
  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. struct scatterlist *sg;
  624. void *sg_buf;
  625. size_t min;
  626. int i, ret;
  627. if (vmalloced_buf || kmap_buf) {
  628. desc_len = min_t(int, max_seg_size, PAGE_SIZE);
  629. sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
  630. } else if (virt_addr_valid(buf)) {
  631. desc_len = min_t(int, max_seg_size, master->max_dma_len);
  632. sgs = DIV_ROUND_UP(len, desc_len);
  633. } else {
  634. return -EINVAL;
  635. }
  636. ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
  637. if (ret != 0)
  638. return ret;
  639. sg = &sgt->sgl[0];
  640. for (i = 0; i < sgs; i++) {
  641. if (vmalloced_buf || kmap_buf) {
  642. min = min_t(size_t,
  643. len, desc_len - offset_in_page(buf));
  644. if (vmalloced_buf)
  645. vm_page = vmalloc_to_page(buf);
  646. else
  647. vm_page = kmap_to_page(buf);
  648. if (!vm_page) {
  649. sg_free_table(sgt);
  650. return -ENOMEM;
  651. }
  652. sg_set_page(sg, vm_page,
  653. min, offset_in_page(buf));
  654. } else {
  655. min = min_t(size_t, len, desc_len);
  656. sg_buf = buf;
  657. sg_set_buf(sg, sg_buf, min);
  658. }
  659. buf += min;
  660. len -= min;
  661. sg = sg_next(sg);
  662. }
  663. ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
  664. if (!ret)
  665. ret = -ENOMEM;
  666. if (ret < 0) {
  667. sg_free_table(sgt);
  668. return ret;
  669. }
  670. sgt->nents = ret;
  671. return 0;
  672. }
  673. static void spi_unmap_buf(struct spi_master *master, struct device *dev,
  674. struct sg_table *sgt, enum dma_data_direction dir)
  675. {
  676. if (sgt->orig_nents) {
  677. dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
  678. sg_free_table(sgt);
  679. }
  680. }
  681. static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
  682. {
  683. struct device *tx_dev, *rx_dev;
  684. struct spi_transfer *xfer;
  685. int ret;
  686. if (!master->can_dma)
  687. return 0;
  688. if (master->dma_tx)
  689. tx_dev = master->dma_tx->device->dev;
  690. else
  691. tx_dev = master->dev.parent;
  692. if (master->dma_rx)
  693. rx_dev = master->dma_rx->device->dev;
  694. else
  695. rx_dev = master->dev.parent;
  696. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  697. if (!master->can_dma(master, msg->spi, xfer))
  698. continue;
  699. if (xfer->tx_buf != NULL) {
  700. ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
  701. (void *)xfer->tx_buf, xfer->len,
  702. DMA_TO_DEVICE);
  703. if (ret != 0)
  704. return ret;
  705. }
  706. if (xfer->rx_buf != NULL) {
  707. ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
  708. xfer->rx_buf, xfer->len,
  709. DMA_FROM_DEVICE);
  710. if (ret != 0) {
  711. spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
  712. DMA_TO_DEVICE);
  713. return ret;
  714. }
  715. }
  716. }
  717. master->cur_msg_mapped = true;
  718. return 0;
  719. }
  720. static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
  721. {
  722. struct spi_transfer *xfer;
  723. struct device *tx_dev, *rx_dev;
  724. if (!master->cur_msg_mapped || !master->can_dma)
  725. return 0;
  726. if (master->dma_tx)
  727. tx_dev = master->dma_tx->device->dev;
  728. else
  729. tx_dev = master->dev.parent;
  730. if (master->dma_rx)
  731. rx_dev = master->dma_rx->device->dev;
  732. else
  733. rx_dev = master->dev.parent;
  734. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  735. if (!master->can_dma(master, msg->spi, xfer))
  736. continue;
  737. spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
  738. spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
  739. }
  740. return 0;
  741. }
  742. #else /* !CONFIG_HAS_DMA */
  743. static inline int spi_map_buf(struct spi_master *master,
  744. struct device *dev, struct sg_table *sgt,
  745. void *buf, size_t len,
  746. enum dma_data_direction dir)
  747. {
  748. return -EINVAL;
  749. }
  750. static inline void spi_unmap_buf(struct spi_master *master,
  751. struct device *dev, struct sg_table *sgt,
  752. enum dma_data_direction dir)
  753. {
  754. }
  755. static inline int __spi_map_msg(struct spi_master *master,
  756. struct spi_message *msg)
  757. {
  758. return 0;
  759. }
  760. static inline int __spi_unmap_msg(struct spi_master *master,
  761. struct spi_message *msg)
  762. {
  763. return 0;
  764. }
  765. #endif /* !CONFIG_HAS_DMA */
  766. static inline int spi_unmap_msg(struct spi_master *master,
  767. struct spi_message *msg)
  768. {
  769. struct spi_transfer *xfer;
  770. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  771. /*
  772. * Restore the original value of tx_buf or rx_buf if they are
  773. * NULL.
  774. */
  775. if (xfer->tx_buf == master->dummy_tx)
  776. xfer->tx_buf = NULL;
  777. if (xfer->rx_buf == master->dummy_rx)
  778. xfer->rx_buf = NULL;
  779. }
  780. return __spi_unmap_msg(master, msg);
  781. }
  782. static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
  783. {
  784. struct spi_transfer *xfer;
  785. void *tmp;
  786. unsigned int max_tx, max_rx;
  787. if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
  788. max_tx = 0;
  789. max_rx = 0;
  790. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  791. if ((master->flags & SPI_MASTER_MUST_TX) &&
  792. !xfer->tx_buf)
  793. max_tx = max(xfer->len, max_tx);
  794. if ((master->flags & SPI_MASTER_MUST_RX) &&
  795. !xfer->rx_buf)
  796. max_rx = max(xfer->len, max_rx);
  797. }
  798. if (max_tx) {
  799. tmp = krealloc(master->dummy_tx, max_tx,
  800. GFP_KERNEL | GFP_DMA);
  801. if (!tmp)
  802. return -ENOMEM;
  803. master->dummy_tx = tmp;
  804. memset(tmp, 0, max_tx);
  805. }
  806. if (max_rx) {
  807. tmp = krealloc(master->dummy_rx, max_rx,
  808. GFP_KERNEL | GFP_DMA);
  809. if (!tmp)
  810. return -ENOMEM;
  811. master->dummy_rx = tmp;
  812. }
  813. if (max_tx || max_rx) {
  814. list_for_each_entry(xfer, &msg->transfers,
  815. transfer_list) {
  816. if (!xfer->tx_buf)
  817. xfer->tx_buf = master->dummy_tx;
  818. if (!xfer->rx_buf)
  819. xfer->rx_buf = master->dummy_rx;
  820. }
  821. }
  822. }
  823. return __spi_map_msg(master, msg);
  824. }
  825. /*
  826. * spi_transfer_one_message - Default implementation of transfer_one_message()
  827. *
  828. * This is a standard implementation of transfer_one_message() for
  829. * drivers which implement a transfer_one() operation. It provides
  830. * standard handling of delays and chip select management.
  831. */
  832. static int spi_transfer_one_message(struct spi_master *master,
  833. struct spi_message *msg)
  834. {
  835. struct spi_transfer *xfer;
  836. bool keep_cs = false;
  837. int ret = 0;
  838. unsigned long long ms = 1;
  839. struct spi_statistics *statm = &master->statistics;
  840. struct spi_statistics *stats = &msg->spi->statistics;
  841. spi_set_cs(msg->spi, true);
  842. SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
  843. SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
  844. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  845. trace_spi_transfer_start(msg, xfer);
  846. spi_statistics_add_transfer_stats(statm, xfer, master);
  847. spi_statistics_add_transfer_stats(stats, xfer, master);
  848. if (xfer->tx_buf || xfer->rx_buf) {
  849. reinit_completion(&master->xfer_completion);
  850. ret = master->transfer_one(master, msg->spi, xfer);
  851. if (ret < 0) {
  852. SPI_STATISTICS_INCREMENT_FIELD(statm,
  853. errors);
  854. SPI_STATISTICS_INCREMENT_FIELD(stats,
  855. errors);
  856. dev_err(&msg->spi->dev,
  857. "SPI transfer failed: %d\n", ret);
  858. goto out;
  859. }
  860. if (ret > 0) {
  861. ret = 0;
  862. ms = 8LL * 1000LL * xfer->len;
  863. do_div(ms, xfer->speed_hz);
  864. ms += ms + 200; /* some tolerance */
  865. if (ms > UINT_MAX)
  866. ms = UINT_MAX;
  867. ms = wait_for_completion_timeout(&master->xfer_completion,
  868. msecs_to_jiffies(ms));
  869. }
  870. if (ms == 0) {
  871. SPI_STATISTICS_INCREMENT_FIELD(statm,
  872. timedout);
  873. SPI_STATISTICS_INCREMENT_FIELD(stats,
  874. timedout);
  875. dev_err(&msg->spi->dev,
  876. "SPI transfer timed out\n");
  877. msg->status = -ETIMEDOUT;
  878. }
  879. } else {
  880. if (xfer->len)
  881. dev_err(&msg->spi->dev,
  882. "Bufferless transfer has length %u\n",
  883. xfer->len);
  884. }
  885. trace_spi_transfer_stop(msg, xfer);
  886. if (msg->status != -EINPROGRESS)
  887. goto out;
  888. if (xfer->delay_usecs)
  889. udelay(xfer->delay_usecs);
  890. if (xfer->cs_change) {
  891. if (list_is_last(&xfer->transfer_list,
  892. &msg->transfers)) {
  893. keep_cs = true;
  894. } else {
  895. spi_set_cs(msg->spi, false);
  896. udelay(10);
  897. spi_set_cs(msg->spi, true);
  898. }
  899. }
  900. msg->actual_length += xfer->len;
  901. }
  902. out:
  903. if (ret != 0 || !keep_cs)
  904. spi_set_cs(msg->spi, false);
  905. if (msg->status == -EINPROGRESS)
  906. msg->status = ret;
  907. if (msg->status && master->handle_err)
  908. master->handle_err(master, msg);
  909. spi_res_release(master, msg);
  910. spi_finalize_current_message(master);
  911. return ret;
  912. }
  913. /**
  914. * spi_finalize_current_transfer - report completion of a transfer
  915. * @master: the master reporting completion
  916. *
  917. * Called by SPI drivers using the core transfer_one_message()
  918. * implementation to notify it that the current interrupt driven
  919. * transfer has finished and the next one may be scheduled.
  920. */
  921. void spi_finalize_current_transfer(struct spi_master *master)
  922. {
  923. complete(&master->xfer_completion);
  924. }
  925. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  926. /**
  927. * __spi_pump_messages - function which processes spi message queue
  928. * @master: master to process queue for
  929. * @in_kthread: true if we are in the context of the message pump thread
  930. *
  931. * This function checks if there is any spi message in the queue that
  932. * needs processing and if so call out to the driver to initialize hardware
  933. * and transfer each message.
  934. *
  935. * Note that it is called both from the kthread itself and also from
  936. * inside spi_sync(); the queue extraction handling at the top of the
  937. * function should deal with this safely.
  938. */
  939. static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
  940. {
  941. unsigned long flags;
  942. bool was_busy = false;
  943. int ret;
  944. /* Lock queue */
  945. spin_lock_irqsave(&master->queue_lock, flags);
  946. /* Make sure we are not already running a message */
  947. if (master->cur_msg) {
  948. spin_unlock_irqrestore(&master->queue_lock, flags);
  949. return;
  950. }
  951. /* If another context is idling the device then defer */
  952. if (master->idling) {
  953. kthread_queue_work(&master->kworker, &master->pump_messages);
  954. spin_unlock_irqrestore(&master->queue_lock, flags);
  955. return;
  956. }
  957. /* Check if the queue is idle */
  958. if (list_empty(&master->queue) || !master->running) {
  959. if (!master->busy) {
  960. spin_unlock_irqrestore(&master->queue_lock, flags);
  961. return;
  962. }
  963. /* Only do teardown in the thread */
  964. if (!in_kthread) {
  965. kthread_queue_work(&master->kworker,
  966. &master->pump_messages);
  967. spin_unlock_irqrestore(&master->queue_lock, flags);
  968. return;
  969. }
  970. master->busy = false;
  971. master->idling = true;
  972. spin_unlock_irqrestore(&master->queue_lock, flags);
  973. kfree(master->dummy_rx);
  974. master->dummy_rx = NULL;
  975. kfree(master->dummy_tx);
  976. master->dummy_tx = NULL;
  977. if (master->unprepare_transfer_hardware &&
  978. master->unprepare_transfer_hardware(master))
  979. dev_err(&master->dev,
  980. "failed to unprepare transfer hardware\n");
  981. if (master->auto_runtime_pm) {
  982. pm_runtime_mark_last_busy(master->dev.parent);
  983. pm_runtime_put_autosuspend(master->dev.parent);
  984. }
  985. trace_spi_master_idle(master);
  986. spin_lock_irqsave(&master->queue_lock, flags);
  987. master->idling = false;
  988. spin_unlock_irqrestore(&master->queue_lock, flags);
  989. return;
  990. }
  991. /* Extract head of queue */
  992. master->cur_msg =
  993. list_first_entry(&master->queue, struct spi_message, queue);
  994. list_del_init(&master->cur_msg->queue);
  995. if (master->busy)
  996. was_busy = true;
  997. else
  998. master->busy = true;
  999. spin_unlock_irqrestore(&master->queue_lock, flags);
  1000. mutex_lock(&master->io_mutex);
  1001. if (!was_busy && master->auto_runtime_pm) {
  1002. ret = pm_runtime_get_sync(master->dev.parent);
  1003. if (ret < 0) {
  1004. dev_err(&master->dev, "Failed to power device: %d\n",
  1005. ret);
  1006. mutex_unlock(&master->io_mutex);
  1007. return;
  1008. }
  1009. }
  1010. if (!was_busy)
  1011. trace_spi_master_busy(master);
  1012. if (!was_busy && master->prepare_transfer_hardware) {
  1013. ret = master->prepare_transfer_hardware(master);
  1014. if (ret) {
  1015. dev_err(&master->dev,
  1016. "failed to prepare transfer hardware\n");
  1017. if (master->auto_runtime_pm)
  1018. pm_runtime_put(master->dev.parent);
  1019. mutex_unlock(&master->io_mutex);
  1020. return;
  1021. }
  1022. }
  1023. trace_spi_message_start(master->cur_msg);
  1024. if (master->prepare_message) {
  1025. ret = master->prepare_message(master, master->cur_msg);
  1026. if (ret) {
  1027. dev_err(&master->dev,
  1028. "failed to prepare message: %d\n", ret);
  1029. master->cur_msg->status = ret;
  1030. spi_finalize_current_message(master);
  1031. goto out;
  1032. }
  1033. master->cur_msg_prepared = true;
  1034. }
  1035. ret = spi_map_msg(master, master->cur_msg);
  1036. if (ret) {
  1037. master->cur_msg->status = ret;
  1038. spi_finalize_current_message(master);
  1039. goto out;
  1040. }
  1041. ret = master->transfer_one_message(master, master->cur_msg);
  1042. if (ret) {
  1043. dev_err(&master->dev,
  1044. "failed to transfer one message from queue\n");
  1045. goto out;
  1046. }
  1047. out:
  1048. mutex_unlock(&master->io_mutex);
  1049. /* Prod the scheduler in case transfer_one() was busy waiting */
  1050. if (!ret)
  1051. cond_resched();
  1052. }
  1053. /**
  1054. * spi_pump_messages - kthread work function which processes spi message queue
  1055. * @work: pointer to kthread work struct contained in the master struct
  1056. */
  1057. static void spi_pump_messages(struct kthread_work *work)
  1058. {
  1059. struct spi_master *master =
  1060. container_of(work, struct spi_master, pump_messages);
  1061. __spi_pump_messages(master, true);
  1062. }
  1063. static int spi_init_queue(struct spi_master *master)
  1064. {
  1065. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  1066. master->running = false;
  1067. master->busy = false;
  1068. kthread_init_worker(&master->kworker);
  1069. master->kworker_task = kthread_run(kthread_worker_fn,
  1070. &master->kworker, "%s",
  1071. dev_name(&master->dev));
  1072. if (IS_ERR(master->kworker_task)) {
  1073. dev_err(&master->dev, "failed to create message pump task\n");
  1074. return PTR_ERR(master->kworker_task);
  1075. }
  1076. kthread_init_work(&master->pump_messages, spi_pump_messages);
  1077. /*
  1078. * Master config will indicate if this controller should run the
  1079. * message pump with high (realtime) priority to reduce the transfer
  1080. * latency on the bus by minimising the delay between a transfer
  1081. * request and the scheduling of the message pump thread. Without this
  1082. * setting the message pump thread will remain at default priority.
  1083. */
  1084. if (master->rt) {
  1085. dev_info(&master->dev,
  1086. "will run message pump with realtime priority\n");
  1087. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  1088. }
  1089. return 0;
  1090. }
  1091. /**
  1092. * spi_get_next_queued_message() - called by driver to check for queued
  1093. * messages
  1094. * @master: the master to check for queued messages
  1095. *
  1096. * If there are more messages in the queue, the next message is returned from
  1097. * this call.
  1098. *
  1099. * Return: the next message in the queue, else NULL if the queue is empty.
  1100. */
  1101. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  1102. {
  1103. struct spi_message *next;
  1104. unsigned long flags;
  1105. /* get a pointer to the next message, if any */
  1106. spin_lock_irqsave(&master->queue_lock, flags);
  1107. next = list_first_entry_or_null(&master->queue, struct spi_message,
  1108. queue);
  1109. spin_unlock_irqrestore(&master->queue_lock, flags);
  1110. return next;
  1111. }
  1112. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  1113. /**
  1114. * spi_finalize_current_message() - the current message is complete
  1115. * @master: the master to return the message to
  1116. *
  1117. * Called by the driver to notify the core that the message in the front of the
  1118. * queue is complete and can be removed from the queue.
  1119. */
  1120. void spi_finalize_current_message(struct spi_master *master)
  1121. {
  1122. struct spi_message *mesg;
  1123. unsigned long flags;
  1124. int ret;
  1125. spin_lock_irqsave(&master->queue_lock, flags);
  1126. mesg = master->cur_msg;
  1127. spin_unlock_irqrestore(&master->queue_lock, flags);
  1128. spi_unmap_msg(master, mesg);
  1129. if (master->cur_msg_prepared && master->unprepare_message) {
  1130. ret = master->unprepare_message(master, mesg);
  1131. if (ret) {
  1132. dev_err(&master->dev,
  1133. "failed to unprepare message: %d\n", ret);
  1134. }
  1135. }
  1136. spin_lock_irqsave(&master->queue_lock, flags);
  1137. master->cur_msg = NULL;
  1138. master->cur_msg_prepared = false;
  1139. kthread_queue_work(&master->kworker, &master->pump_messages);
  1140. spin_unlock_irqrestore(&master->queue_lock, flags);
  1141. trace_spi_message_done(mesg);
  1142. mesg->state = NULL;
  1143. if (mesg->complete)
  1144. mesg->complete(mesg->context);
  1145. }
  1146. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  1147. static int spi_start_queue(struct spi_master *master)
  1148. {
  1149. unsigned long flags;
  1150. spin_lock_irqsave(&master->queue_lock, flags);
  1151. if (master->running || master->busy) {
  1152. spin_unlock_irqrestore(&master->queue_lock, flags);
  1153. return -EBUSY;
  1154. }
  1155. master->running = true;
  1156. master->cur_msg = NULL;
  1157. spin_unlock_irqrestore(&master->queue_lock, flags);
  1158. kthread_queue_work(&master->kworker, &master->pump_messages);
  1159. return 0;
  1160. }
  1161. static int spi_stop_queue(struct spi_master *master)
  1162. {
  1163. unsigned long flags;
  1164. unsigned limit = 500;
  1165. int ret = 0;
  1166. spin_lock_irqsave(&master->queue_lock, flags);
  1167. /*
  1168. * This is a bit lame, but is optimized for the common execution path.
  1169. * A wait_queue on the master->busy could be used, but then the common
  1170. * execution path (pump_messages) would be required to call wake_up or
  1171. * friends on every SPI message. Do this instead.
  1172. */
  1173. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  1174. spin_unlock_irqrestore(&master->queue_lock, flags);
  1175. usleep_range(10000, 11000);
  1176. spin_lock_irqsave(&master->queue_lock, flags);
  1177. }
  1178. if (!list_empty(&master->queue) || master->busy)
  1179. ret = -EBUSY;
  1180. else
  1181. master->running = false;
  1182. spin_unlock_irqrestore(&master->queue_lock, flags);
  1183. if (ret) {
  1184. dev_warn(&master->dev,
  1185. "could not stop message queue\n");
  1186. return ret;
  1187. }
  1188. return ret;
  1189. }
  1190. static int spi_destroy_queue(struct spi_master *master)
  1191. {
  1192. int ret;
  1193. ret = spi_stop_queue(master);
  1194. /*
  1195. * kthread_flush_worker will block until all work is done.
  1196. * If the reason that stop_queue timed out is that the work will never
  1197. * finish, then it does no good to call flush/stop thread, so
  1198. * return anyway.
  1199. */
  1200. if (ret) {
  1201. dev_err(&master->dev, "problem destroying queue\n");
  1202. return ret;
  1203. }
  1204. kthread_flush_worker(&master->kworker);
  1205. kthread_stop(master->kworker_task);
  1206. return 0;
  1207. }
  1208. static int __spi_queued_transfer(struct spi_device *spi,
  1209. struct spi_message *msg,
  1210. bool need_pump)
  1211. {
  1212. struct spi_master *master = spi->master;
  1213. unsigned long flags;
  1214. spin_lock_irqsave(&master->queue_lock, flags);
  1215. if (!master->running) {
  1216. spin_unlock_irqrestore(&master->queue_lock, flags);
  1217. return -ESHUTDOWN;
  1218. }
  1219. msg->actual_length = 0;
  1220. msg->status = -EINPROGRESS;
  1221. list_add_tail(&msg->queue, &master->queue);
  1222. if (!master->busy && need_pump)
  1223. kthread_queue_work(&master->kworker, &master->pump_messages);
  1224. spin_unlock_irqrestore(&master->queue_lock, flags);
  1225. return 0;
  1226. }
  1227. /**
  1228. * spi_queued_transfer - transfer function for queued transfers
  1229. * @spi: spi device which is requesting transfer
  1230. * @msg: spi message which is to handled is queued to driver queue
  1231. *
  1232. * Return: zero on success, else a negative error code.
  1233. */
  1234. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  1235. {
  1236. return __spi_queued_transfer(spi, msg, true);
  1237. }
  1238. static int spi_master_initialize_queue(struct spi_master *master)
  1239. {
  1240. int ret;
  1241. master->transfer = spi_queued_transfer;
  1242. if (!master->transfer_one_message)
  1243. master->transfer_one_message = spi_transfer_one_message;
  1244. /* Initialize and start queue */
  1245. ret = spi_init_queue(master);
  1246. if (ret) {
  1247. dev_err(&master->dev, "problem initializing queue\n");
  1248. goto err_init_queue;
  1249. }
  1250. master->queued = true;
  1251. ret = spi_start_queue(master);
  1252. if (ret) {
  1253. dev_err(&master->dev, "problem starting queue\n");
  1254. goto err_start_queue;
  1255. }
  1256. return 0;
  1257. err_start_queue:
  1258. spi_destroy_queue(master);
  1259. err_init_queue:
  1260. return ret;
  1261. }
  1262. /*-------------------------------------------------------------------------*/
  1263. #if defined(CONFIG_OF)
  1264. static struct spi_device *
  1265. of_register_spi_device(struct spi_master *master, struct device_node *nc)
  1266. {
  1267. struct spi_device *spi;
  1268. int rc;
  1269. u32 value;
  1270. /* Alloc an spi_device */
  1271. spi = spi_alloc_device(master);
  1272. if (!spi) {
  1273. dev_err(&master->dev, "spi_device alloc error for %s\n",
  1274. nc->full_name);
  1275. rc = -ENOMEM;
  1276. goto err_out;
  1277. }
  1278. /* Select device driver */
  1279. rc = of_modalias_node(nc, spi->modalias,
  1280. sizeof(spi->modalias));
  1281. if (rc < 0) {
  1282. dev_err(&master->dev, "cannot find modalias for %s\n",
  1283. nc->full_name);
  1284. goto err_out;
  1285. }
  1286. /* Device address */
  1287. rc = of_property_read_u32(nc, "reg", &value);
  1288. if (rc) {
  1289. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  1290. nc->full_name, rc);
  1291. goto err_out;
  1292. }
  1293. spi->chip_select = value;
  1294. /* Mode (clock phase/polarity/etc.) */
  1295. if (of_find_property(nc, "spi-cpha", NULL))
  1296. spi->mode |= SPI_CPHA;
  1297. if (of_find_property(nc, "spi-cpol", NULL))
  1298. spi->mode |= SPI_CPOL;
  1299. if (of_find_property(nc, "spi-cs-high", NULL))
  1300. spi->mode |= SPI_CS_HIGH;
  1301. if (of_find_property(nc, "spi-3wire", NULL))
  1302. spi->mode |= SPI_3WIRE;
  1303. if (of_find_property(nc, "spi-lsb-first", NULL))
  1304. spi->mode |= SPI_LSB_FIRST;
  1305. /* Device DUAL/QUAD mode */
  1306. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  1307. switch (value) {
  1308. case 1:
  1309. break;
  1310. case 2:
  1311. spi->mode |= SPI_TX_DUAL;
  1312. break;
  1313. case 4:
  1314. spi->mode |= SPI_TX_QUAD;
  1315. break;
  1316. default:
  1317. dev_warn(&master->dev,
  1318. "spi-tx-bus-width %d not supported\n",
  1319. value);
  1320. break;
  1321. }
  1322. }
  1323. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  1324. switch (value) {
  1325. case 1:
  1326. break;
  1327. case 2:
  1328. spi->mode |= SPI_RX_DUAL;
  1329. break;
  1330. case 4:
  1331. spi->mode |= SPI_RX_QUAD;
  1332. break;
  1333. default:
  1334. dev_warn(&master->dev,
  1335. "spi-rx-bus-width %d not supported\n",
  1336. value);
  1337. break;
  1338. }
  1339. }
  1340. /* Device speed */
  1341. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  1342. if (rc) {
  1343. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  1344. nc->full_name, rc);
  1345. goto err_out;
  1346. }
  1347. spi->max_speed_hz = value;
  1348. /* Store a pointer to the node in the device structure */
  1349. of_node_get(nc);
  1350. spi->dev.of_node = nc;
  1351. /* Register the new device */
  1352. rc = spi_add_device(spi);
  1353. if (rc) {
  1354. dev_err(&master->dev, "spi_device register error %s\n",
  1355. nc->full_name);
  1356. goto err_of_node_put;
  1357. }
  1358. return spi;
  1359. err_of_node_put:
  1360. of_node_put(nc);
  1361. err_out:
  1362. spi_dev_put(spi);
  1363. return ERR_PTR(rc);
  1364. }
  1365. /**
  1366. * of_register_spi_devices() - Register child devices onto the SPI bus
  1367. * @master: Pointer to spi_master device
  1368. *
  1369. * Registers an spi_device for each child node of master node which has a 'reg'
  1370. * property.
  1371. */
  1372. static void of_register_spi_devices(struct spi_master *master)
  1373. {
  1374. struct spi_device *spi;
  1375. struct device_node *nc;
  1376. if (!master->dev.of_node)
  1377. return;
  1378. for_each_available_child_of_node(master->dev.of_node, nc) {
  1379. if (of_node_test_and_set_flag(nc, OF_POPULATED))
  1380. continue;
  1381. spi = of_register_spi_device(master, nc);
  1382. if (IS_ERR(spi)) {
  1383. dev_warn(&master->dev, "Failed to create SPI device for %s\n",
  1384. nc->full_name);
  1385. of_node_clear_flag(nc, OF_POPULATED);
  1386. }
  1387. }
  1388. }
  1389. #else
  1390. static void of_register_spi_devices(struct spi_master *master) { }
  1391. #endif
  1392. #ifdef CONFIG_ACPI
  1393. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  1394. {
  1395. struct spi_device *spi = data;
  1396. struct spi_master *master = spi->master;
  1397. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  1398. struct acpi_resource_spi_serialbus *sb;
  1399. sb = &ares->data.spi_serial_bus;
  1400. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  1401. /*
  1402. * ACPI DeviceSelection numbering is handled by the
  1403. * host controller driver in Windows and can vary
  1404. * from driver to driver. In Linux we always expect
  1405. * 0 .. max - 1 so we need to ask the driver to
  1406. * translate between the two schemes.
  1407. */
  1408. if (master->fw_translate_cs) {
  1409. int cs = master->fw_translate_cs(master,
  1410. sb->device_selection);
  1411. if (cs < 0)
  1412. return cs;
  1413. spi->chip_select = cs;
  1414. } else {
  1415. spi->chip_select = sb->device_selection;
  1416. }
  1417. spi->max_speed_hz = sb->connection_speed;
  1418. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  1419. spi->mode |= SPI_CPHA;
  1420. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  1421. spi->mode |= SPI_CPOL;
  1422. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  1423. spi->mode |= SPI_CS_HIGH;
  1424. }
  1425. } else if (spi->irq < 0) {
  1426. struct resource r;
  1427. if (acpi_dev_resource_interrupt(ares, 0, &r))
  1428. spi->irq = r.start;
  1429. }
  1430. /* Always tell the ACPI core to skip this resource */
  1431. return 1;
  1432. }
  1433. static acpi_status acpi_register_spi_device(struct spi_master *master,
  1434. struct acpi_device *adev)
  1435. {
  1436. struct list_head resource_list;
  1437. struct spi_device *spi;
  1438. int ret;
  1439. if (acpi_bus_get_status(adev) || !adev->status.present ||
  1440. acpi_device_enumerated(adev))
  1441. return AE_OK;
  1442. spi = spi_alloc_device(master);
  1443. if (!spi) {
  1444. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  1445. dev_name(&adev->dev));
  1446. return AE_NO_MEMORY;
  1447. }
  1448. ACPI_COMPANION_SET(&spi->dev, adev);
  1449. spi->irq = -1;
  1450. INIT_LIST_HEAD(&resource_list);
  1451. ret = acpi_dev_get_resources(adev, &resource_list,
  1452. acpi_spi_add_resource, spi);
  1453. acpi_dev_free_resource_list(&resource_list);
  1454. if (ret < 0 || !spi->max_speed_hz) {
  1455. spi_dev_put(spi);
  1456. return AE_OK;
  1457. }
  1458. if (spi->irq < 0)
  1459. spi->irq = acpi_dev_gpio_irq_get(adev, 0);
  1460. acpi_device_set_enumerated(adev);
  1461. adev->power.flags.ignore_parent = true;
  1462. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  1463. if (spi_add_device(spi)) {
  1464. adev->power.flags.ignore_parent = false;
  1465. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  1466. dev_name(&adev->dev));
  1467. spi_dev_put(spi);
  1468. }
  1469. return AE_OK;
  1470. }
  1471. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  1472. void *data, void **return_value)
  1473. {
  1474. struct spi_master *master = data;
  1475. struct acpi_device *adev;
  1476. if (acpi_bus_get_device(handle, &adev))
  1477. return AE_OK;
  1478. return acpi_register_spi_device(master, adev);
  1479. }
  1480. static void acpi_register_spi_devices(struct spi_master *master)
  1481. {
  1482. acpi_status status;
  1483. acpi_handle handle;
  1484. handle = ACPI_HANDLE(master->dev.parent);
  1485. if (!handle)
  1486. return;
  1487. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1488. acpi_spi_add_device, NULL,
  1489. master, NULL);
  1490. if (ACPI_FAILURE(status))
  1491. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1492. }
  1493. #else
  1494. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1495. #endif /* CONFIG_ACPI */
  1496. static void spi_master_release(struct device *dev)
  1497. {
  1498. struct spi_master *master;
  1499. master = container_of(dev, struct spi_master, dev);
  1500. kfree(master);
  1501. }
  1502. static struct class spi_master_class = {
  1503. .name = "spi_master",
  1504. .owner = THIS_MODULE,
  1505. .dev_release = spi_master_release,
  1506. .dev_groups = spi_master_groups,
  1507. };
  1508. /**
  1509. * spi_alloc_master - allocate SPI master controller
  1510. * @dev: the controller, possibly using the platform_bus
  1511. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1512. * memory is in the driver_data field of the returned device,
  1513. * accessible with spi_master_get_devdata().
  1514. * Context: can sleep
  1515. *
  1516. * This call is used only by SPI master controller drivers, which are the
  1517. * only ones directly touching chip registers. It's how they allocate
  1518. * an spi_master structure, prior to calling spi_register_master().
  1519. *
  1520. * This must be called from context that can sleep.
  1521. *
  1522. * The caller is responsible for assigning the bus number and initializing
  1523. * the master's methods before calling spi_register_master(); and (after errors
  1524. * adding the device) calling spi_master_put() to prevent a memory leak.
  1525. *
  1526. * Return: the SPI master structure on success, else NULL.
  1527. */
  1528. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1529. {
  1530. struct spi_master *master;
  1531. if (!dev)
  1532. return NULL;
  1533. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1534. if (!master)
  1535. return NULL;
  1536. device_initialize(&master->dev);
  1537. master->bus_num = -1;
  1538. master->num_chipselect = 1;
  1539. master->dev.class = &spi_master_class;
  1540. master->dev.parent = dev;
  1541. pm_suspend_ignore_children(&master->dev, true);
  1542. spi_master_set_devdata(master, &master[1]);
  1543. return master;
  1544. }
  1545. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1546. #ifdef CONFIG_OF
  1547. static int of_spi_register_master(struct spi_master *master)
  1548. {
  1549. int nb, i, *cs;
  1550. struct device_node *np = master->dev.of_node;
  1551. if (!np)
  1552. return 0;
  1553. nb = of_gpio_named_count(np, "cs-gpios");
  1554. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1555. /* Return error only for an incorrectly formed cs-gpios property */
  1556. if (nb == 0 || nb == -ENOENT)
  1557. return 0;
  1558. else if (nb < 0)
  1559. return nb;
  1560. cs = devm_kzalloc(&master->dev,
  1561. sizeof(int) * master->num_chipselect,
  1562. GFP_KERNEL);
  1563. master->cs_gpios = cs;
  1564. if (!master->cs_gpios)
  1565. return -ENOMEM;
  1566. for (i = 0; i < master->num_chipselect; i++)
  1567. cs[i] = -ENOENT;
  1568. for (i = 0; i < nb; i++)
  1569. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1570. return 0;
  1571. }
  1572. #else
  1573. static int of_spi_register_master(struct spi_master *master)
  1574. {
  1575. return 0;
  1576. }
  1577. #endif
  1578. /**
  1579. * spi_register_master - register SPI master controller
  1580. * @master: initialized master, originally from spi_alloc_master()
  1581. * Context: can sleep
  1582. *
  1583. * SPI master controllers connect to their drivers using some non-SPI bus,
  1584. * such as the platform bus. The final stage of probe() in that code
  1585. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1586. *
  1587. * SPI controllers use board specific (often SOC specific) bus numbers,
  1588. * and board-specific addressing for SPI devices combines those numbers
  1589. * with chip select numbers. Since SPI does not directly support dynamic
  1590. * device identification, boards need configuration tables telling which
  1591. * chip is at which address.
  1592. *
  1593. * This must be called from context that can sleep. It returns zero on
  1594. * success, else a negative error code (dropping the master's refcount).
  1595. * After a successful return, the caller is responsible for calling
  1596. * spi_unregister_master().
  1597. *
  1598. * Return: zero on success, else a negative error code.
  1599. */
  1600. int spi_register_master(struct spi_master *master)
  1601. {
  1602. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1603. struct device *dev = master->dev.parent;
  1604. struct boardinfo *bi;
  1605. int status = -ENODEV;
  1606. int dynamic = 0;
  1607. if (!dev)
  1608. return -ENODEV;
  1609. status = of_spi_register_master(master);
  1610. if (status)
  1611. return status;
  1612. /* even if it's just one always-selected device, there must
  1613. * be at least one chipselect
  1614. */
  1615. if (master->num_chipselect == 0)
  1616. return -EINVAL;
  1617. if ((master->bus_num < 0) && master->dev.of_node)
  1618. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1619. /* convention: dynamically assigned bus IDs count down from the max */
  1620. if (master->bus_num < 0) {
  1621. /* FIXME switch to an IDR based scheme, something like
  1622. * I2C now uses, so we can't run out of "dynamic" IDs
  1623. */
  1624. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1625. dynamic = 1;
  1626. }
  1627. INIT_LIST_HEAD(&master->queue);
  1628. spin_lock_init(&master->queue_lock);
  1629. spin_lock_init(&master->bus_lock_spinlock);
  1630. mutex_init(&master->bus_lock_mutex);
  1631. mutex_init(&master->io_mutex);
  1632. master->bus_lock_flag = 0;
  1633. init_completion(&master->xfer_completion);
  1634. if (!master->max_dma_len)
  1635. master->max_dma_len = INT_MAX;
  1636. /* register the device, then userspace will see it.
  1637. * registration fails if the bus ID is in use.
  1638. */
  1639. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1640. status = device_add(&master->dev);
  1641. if (status < 0)
  1642. goto done;
  1643. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1644. dynamic ? " (dynamic)" : "");
  1645. /* If we're using a queued driver, start the queue */
  1646. if (master->transfer)
  1647. dev_info(dev, "master is unqueued, this is deprecated\n");
  1648. else {
  1649. status = spi_master_initialize_queue(master);
  1650. if (status) {
  1651. device_del(&master->dev);
  1652. goto done;
  1653. }
  1654. }
  1655. /* add statistics */
  1656. spin_lock_init(&master->statistics.lock);
  1657. mutex_lock(&board_lock);
  1658. list_add_tail(&master->list, &spi_master_list);
  1659. list_for_each_entry(bi, &board_list, list)
  1660. spi_match_master_to_boardinfo(master, &bi->board_info);
  1661. mutex_unlock(&board_lock);
  1662. /* Register devices from the device tree and ACPI */
  1663. of_register_spi_devices(master);
  1664. acpi_register_spi_devices(master);
  1665. done:
  1666. return status;
  1667. }
  1668. EXPORT_SYMBOL_GPL(spi_register_master);
  1669. static void devm_spi_unregister(struct device *dev, void *res)
  1670. {
  1671. spi_unregister_master(*(struct spi_master **)res);
  1672. }
  1673. /**
  1674. * dev_spi_register_master - register managed SPI master controller
  1675. * @dev: device managing SPI master
  1676. * @master: initialized master, originally from spi_alloc_master()
  1677. * Context: can sleep
  1678. *
  1679. * Register a SPI device as with spi_register_master() which will
  1680. * automatically be unregister
  1681. *
  1682. * Return: zero on success, else a negative error code.
  1683. */
  1684. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1685. {
  1686. struct spi_master **ptr;
  1687. int ret;
  1688. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1689. if (!ptr)
  1690. return -ENOMEM;
  1691. ret = spi_register_master(master);
  1692. if (!ret) {
  1693. *ptr = master;
  1694. devres_add(dev, ptr);
  1695. } else {
  1696. devres_free(ptr);
  1697. }
  1698. return ret;
  1699. }
  1700. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1701. static int __unregister(struct device *dev, void *null)
  1702. {
  1703. spi_unregister_device(to_spi_device(dev));
  1704. return 0;
  1705. }
  1706. /**
  1707. * spi_unregister_master - unregister SPI master controller
  1708. * @master: the master being unregistered
  1709. * Context: can sleep
  1710. *
  1711. * This call is used only by SPI master controller drivers, which are the
  1712. * only ones directly touching chip registers.
  1713. *
  1714. * This must be called from context that can sleep.
  1715. */
  1716. void spi_unregister_master(struct spi_master *master)
  1717. {
  1718. int dummy;
  1719. if (master->queued) {
  1720. if (spi_destroy_queue(master))
  1721. dev_err(&master->dev, "queue remove failed\n");
  1722. }
  1723. mutex_lock(&board_lock);
  1724. list_del(&master->list);
  1725. mutex_unlock(&board_lock);
  1726. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1727. device_unregister(&master->dev);
  1728. }
  1729. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1730. int spi_master_suspend(struct spi_master *master)
  1731. {
  1732. int ret;
  1733. /* Basically no-ops for non-queued masters */
  1734. if (!master->queued)
  1735. return 0;
  1736. ret = spi_stop_queue(master);
  1737. if (ret)
  1738. dev_err(&master->dev, "queue stop failed\n");
  1739. return ret;
  1740. }
  1741. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1742. int spi_master_resume(struct spi_master *master)
  1743. {
  1744. int ret;
  1745. if (!master->queued)
  1746. return 0;
  1747. ret = spi_start_queue(master);
  1748. if (ret)
  1749. dev_err(&master->dev, "queue restart failed\n");
  1750. return ret;
  1751. }
  1752. EXPORT_SYMBOL_GPL(spi_master_resume);
  1753. static int __spi_master_match(struct device *dev, const void *data)
  1754. {
  1755. struct spi_master *m;
  1756. const u16 *bus_num = data;
  1757. m = container_of(dev, struct spi_master, dev);
  1758. return m->bus_num == *bus_num;
  1759. }
  1760. /**
  1761. * spi_busnum_to_master - look up master associated with bus_num
  1762. * @bus_num: the master's bus number
  1763. * Context: can sleep
  1764. *
  1765. * This call may be used with devices that are registered after
  1766. * arch init time. It returns a refcounted pointer to the relevant
  1767. * spi_master (which the caller must release), or NULL if there is
  1768. * no such master registered.
  1769. *
  1770. * Return: the SPI master structure on success, else NULL.
  1771. */
  1772. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1773. {
  1774. struct device *dev;
  1775. struct spi_master *master = NULL;
  1776. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1777. __spi_master_match);
  1778. if (dev)
  1779. master = container_of(dev, struct spi_master, dev);
  1780. /* reference got in class_find_device */
  1781. return master;
  1782. }
  1783. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1784. /*-------------------------------------------------------------------------*/
  1785. /* Core methods for SPI resource management */
  1786. /**
  1787. * spi_res_alloc - allocate a spi resource that is life-cycle managed
  1788. * during the processing of a spi_message while using
  1789. * spi_transfer_one
  1790. * @spi: the spi device for which we allocate memory
  1791. * @release: the release code to execute for this resource
  1792. * @size: size to alloc and return
  1793. * @gfp: GFP allocation flags
  1794. *
  1795. * Return: the pointer to the allocated data
  1796. *
  1797. * This may get enhanced in the future to allocate from a memory pool
  1798. * of the @spi_device or @spi_master to avoid repeated allocations.
  1799. */
  1800. void *spi_res_alloc(struct spi_device *spi,
  1801. spi_res_release_t release,
  1802. size_t size, gfp_t gfp)
  1803. {
  1804. struct spi_res *sres;
  1805. sres = kzalloc(sizeof(*sres) + size, gfp);
  1806. if (!sres)
  1807. return NULL;
  1808. INIT_LIST_HEAD(&sres->entry);
  1809. sres->release = release;
  1810. return sres->data;
  1811. }
  1812. EXPORT_SYMBOL_GPL(spi_res_alloc);
  1813. /**
  1814. * spi_res_free - free an spi resource
  1815. * @res: pointer to the custom data of a resource
  1816. *
  1817. */
  1818. void spi_res_free(void *res)
  1819. {
  1820. struct spi_res *sres = container_of(res, struct spi_res, data);
  1821. if (!res)
  1822. return;
  1823. WARN_ON(!list_empty(&sres->entry));
  1824. kfree(sres);
  1825. }
  1826. EXPORT_SYMBOL_GPL(spi_res_free);
  1827. /**
  1828. * spi_res_add - add a spi_res to the spi_message
  1829. * @message: the spi message
  1830. * @res: the spi_resource
  1831. */
  1832. void spi_res_add(struct spi_message *message, void *res)
  1833. {
  1834. struct spi_res *sres = container_of(res, struct spi_res, data);
  1835. WARN_ON(!list_empty(&sres->entry));
  1836. list_add_tail(&sres->entry, &message->resources);
  1837. }
  1838. EXPORT_SYMBOL_GPL(spi_res_add);
  1839. /**
  1840. * spi_res_release - release all spi resources for this message
  1841. * @master: the @spi_master
  1842. * @message: the @spi_message
  1843. */
  1844. void spi_res_release(struct spi_master *master,
  1845. struct spi_message *message)
  1846. {
  1847. struct spi_res *res;
  1848. while (!list_empty(&message->resources)) {
  1849. res = list_last_entry(&message->resources,
  1850. struct spi_res, entry);
  1851. if (res->release)
  1852. res->release(master, message, res->data);
  1853. list_del(&res->entry);
  1854. kfree(res);
  1855. }
  1856. }
  1857. EXPORT_SYMBOL_GPL(spi_res_release);
  1858. /*-------------------------------------------------------------------------*/
  1859. /* Core methods for spi_message alterations */
  1860. static void __spi_replace_transfers_release(struct spi_master *master,
  1861. struct spi_message *msg,
  1862. void *res)
  1863. {
  1864. struct spi_replaced_transfers *rxfer = res;
  1865. size_t i;
  1866. /* call extra callback if requested */
  1867. if (rxfer->release)
  1868. rxfer->release(master, msg, res);
  1869. /* insert replaced transfers back into the message */
  1870. list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
  1871. /* remove the formerly inserted entries */
  1872. for (i = 0; i < rxfer->inserted; i++)
  1873. list_del(&rxfer->inserted_transfers[i].transfer_list);
  1874. }
  1875. /**
  1876. * spi_replace_transfers - replace transfers with several transfers
  1877. * and register change with spi_message.resources
  1878. * @msg: the spi_message we work upon
  1879. * @xfer_first: the first spi_transfer we want to replace
  1880. * @remove: number of transfers to remove
  1881. * @insert: the number of transfers we want to insert instead
  1882. * @release: extra release code necessary in some circumstances
  1883. * @extradatasize: extra data to allocate (with alignment guarantees
  1884. * of struct @spi_transfer)
  1885. * @gfp: gfp flags
  1886. *
  1887. * Returns: pointer to @spi_replaced_transfers,
  1888. * PTR_ERR(...) in case of errors.
  1889. */
  1890. struct spi_replaced_transfers *spi_replace_transfers(
  1891. struct spi_message *msg,
  1892. struct spi_transfer *xfer_first,
  1893. size_t remove,
  1894. size_t insert,
  1895. spi_replaced_release_t release,
  1896. size_t extradatasize,
  1897. gfp_t gfp)
  1898. {
  1899. struct spi_replaced_transfers *rxfer;
  1900. struct spi_transfer *xfer;
  1901. size_t i;
  1902. /* allocate the structure using spi_res */
  1903. rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
  1904. insert * sizeof(struct spi_transfer)
  1905. + sizeof(struct spi_replaced_transfers)
  1906. + extradatasize,
  1907. gfp);
  1908. if (!rxfer)
  1909. return ERR_PTR(-ENOMEM);
  1910. /* the release code to invoke before running the generic release */
  1911. rxfer->release = release;
  1912. /* assign extradata */
  1913. if (extradatasize)
  1914. rxfer->extradata =
  1915. &rxfer->inserted_transfers[insert];
  1916. /* init the replaced_transfers list */
  1917. INIT_LIST_HEAD(&rxfer->replaced_transfers);
  1918. /* assign the list_entry after which we should reinsert
  1919. * the @replaced_transfers - it may be spi_message.messages!
  1920. */
  1921. rxfer->replaced_after = xfer_first->transfer_list.prev;
  1922. /* remove the requested number of transfers */
  1923. for (i = 0; i < remove; i++) {
  1924. /* if the entry after replaced_after it is msg->transfers
  1925. * then we have been requested to remove more transfers
  1926. * than are in the list
  1927. */
  1928. if (rxfer->replaced_after->next == &msg->transfers) {
  1929. dev_err(&msg->spi->dev,
  1930. "requested to remove more spi_transfers than are available\n");
  1931. /* insert replaced transfers back into the message */
  1932. list_splice(&rxfer->replaced_transfers,
  1933. rxfer->replaced_after);
  1934. /* free the spi_replace_transfer structure */
  1935. spi_res_free(rxfer);
  1936. /* and return with an error */
  1937. return ERR_PTR(-EINVAL);
  1938. }
  1939. /* remove the entry after replaced_after from list of
  1940. * transfers and add it to list of replaced_transfers
  1941. */
  1942. list_move_tail(rxfer->replaced_after->next,
  1943. &rxfer->replaced_transfers);
  1944. }
  1945. /* create copy of the given xfer with identical settings
  1946. * based on the first transfer to get removed
  1947. */
  1948. for (i = 0; i < insert; i++) {
  1949. /* we need to run in reverse order */
  1950. xfer = &rxfer->inserted_transfers[insert - 1 - i];
  1951. /* copy all spi_transfer data */
  1952. memcpy(xfer, xfer_first, sizeof(*xfer));
  1953. /* add to list */
  1954. list_add(&xfer->transfer_list, rxfer->replaced_after);
  1955. /* clear cs_change and delay_usecs for all but the last */
  1956. if (i) {
  1957. xfer->cs_change = false;
  1958. xfer->delay_usecs = 0;
  1959. }
  1960. }
  1961. /* set up inserted */
  1962. rxfer->inserted = insert;
  1963. /* and register it with spi_res/spi_message */
  1964. spi_res_add(msg, rxfer);
  1965. return rxfer;
  1966. }
  1967. EXPORT_SYMBOL_GPL(spi_replace_transfers);
  1968. static int __spi_split_transfer_maxsize(struct spi_master *master,
  1969. struct spi_message *msg,
  1970. struct spi_transfer **xferp,
  1971. size_t maxsize,
  1972. gfp_t gfp)
  1973. {
  1974. struct spi_transfer *xfer = *xferp, *xfers;
  1975. struct spi_replaced_transfers *srt;
  1976. size_t offset;
  1977. size_t count, i;
  1978. /* warn once about this fact that we are splitting a transfer */
  1979. dev_warn_once(&msg->spi->dev,
  1980. "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
  1981. xfer->len, maxsize);
  1982. /* calculate how many we have to replace */
  1983. count = DIV_ROUND_UP(xfer->len, maxsize);
  1984. /* create replacement */
  1985. srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
  1986. if (IS_ERR(srt))
  1987. return PTR_ERR(srt);
  1988. xfers = srt->inserted_transfers;
  1989. /* now handle each of those newly inserted spi_transfers
  1990. * note that the replacements spi_transfers all are preset
  1991. * to the same values as *xferp, so tx_buf, rx_buf and len
  1992. * are all identical (as well as most others)
  1993. * so we just have to fix up len and the pointers.
  1994. *
  1995. * this also includes support for the depreciated
  1996. * spi_message.is_dma_mapped interface
  1997. */
  1998. /* the first transfer just needs the length modified, so we
  1999. * run it outside the loop
  2000. */
  2001. xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
  2002. /* all the others need rx_buf/tx_buf also set */
  2003. for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
  2004. /* update rx_buf, tx_buf and dma */
  2005. if (xfers[i].rx_buf)
  2006. xfers[i].rx_buf += offset;
  2007. if (xfers[i].rx_dma)
  2008. xfers[i].rx_dma += offset;
  2009. if (xfers[i].tx_buf)
  2010. xfers[i].tx_buf += offset;
  2011. if (xfers[i].tx_dma)
  2012. xfers[i].tx_dma += offset;
  2013. /* update length */
  2014. xfers[i].len = min(maxsize, xfers[i].len - offset);
  2015. }
  2016. /* we set up xferp to the last entry we have inserted,
  2017. * so that we skip those already split transfers
  2018. */
  2019. *xferp = &xfers[count - 1];
  2020. /* increment statistics counters */
  2021. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
  2022. transfers_split_maxsize);
  2023. SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
  2024. transfers_split_maxsize);
  2025. return 0;
  2026. }
  2027. /**
  2028. * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
  2029. * when an individual transfer exceeds a
  2030. * certain size
  2031. * @master: the @spi_master for this transfer
  2032. * @msg: the @spi_message to transform
  2033. * @maxsize: the maximum when to apply this
  2034. * @gfp: GFP allocation flags
  2035. *
  2036. * Return: status of transformation
  2037. */
  2038. int spi_split_transfers_maxsize(struct spi_master *master,
  2039. struct spi_message *msg,
  2040. size_t maxsize,
  2041. gfp_t gfp)
  2042. {
  2043. struct spi_transfer *xfer;
  2044. int ret;
  2045. /* iterate over the transfer_list,
  2046. * but note that xfer is advanced to the last transfer inserted
  2047. * to avoid checking sizes again unnecessarily (also xfer does
  2048. * potentiall belong to a different list by the time the
  2049. * replacement has happened
  2050. */
  2051. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  2052. if (xfer->len > maxsize) {
  2053. ret = __spi_split_transfer_maxsize(
  2054. master, msg, &xfer, maxsize, gfp);
  2055. if (ret)
  2056. return ret;
  2057. }
  2058. }
  2059. return 0;
  2060. }
  2061. EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
  2062. /*-------------------------------------------------------------------------*/
  2063. /* Core methods for SPI master protocol drivers. Some of the
  2064. * other core methods are currently defined as inline functions.
  2065. */
  2066. static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
  2067. {
  2068. if (master->bits_per_word_mask) {
  2069. /* Only 32 bits fit in the mask */
  2070. if (bits_per_word > 32)
  2071. return -EINVAL;
  2072. if (!(master->bits_per_word_mask &
  2073. SPI_BPW_MASK(bits_per_word)))
  2074. return -EINVAL;
  2075. }
  2076. return 0;
  2077. }
  2078. /**
  2079. * spi_setup - setup SPI mode and clock rate
  2080. * @spi: the device whose settings are being modified
  2081. * Context: can sleep, and no requests are queued to the device
  2082. *
  2083. * SPI protocol drivers may need to update the transfer mode if the
  2084. * device doesn't work with its default. They may likewise need
  2085. * to update clock rates or word sizes from initial values. This function
  2086. * changes those settings, and must be called from a context that can sleep.
  2087. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  2088. * effect the next time the device is selected and data is transferred to
  2089. * or from it. When this function returns, the spi device is deselected.
  2090. *
  2091. * Note that this call will fail if the protocol driver specifies an option
  2092. * that the underlying controller or its driver does not support. For
  2093. * example, not all hardware supports wire transfers using nine bit words,
  2094. * LSB-first wire encoding, or active-high chipselects.
  2095. *
  2096. * Return: zero on success, else a negative error code.
  2097. */
  2098. int spi_setup(struct spi_device *spi)
  2099. {
  2100. unsigned bad_bits, ugly_bits;
  2101. int status;
  2102. /* check mode to prevent that DUAL and QUAD set at the same time
  2103. */
  2104. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  2105. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  2106. dev_err(&spi->dev,
  2107. "setup: can not select dual and quad at the same time\n");
  2108. return -EINVAL;
  2109. }
  2110. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  2111. */
  2112. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  2113. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  2114. return -EINVAL;
  2115. /* help drivers fail *cleanly* when they need options
  2116. * that aren't supported with their current master
  2117. */
  2118. bad_bits = spi->mode & ~spi->master->mode_bits;
  2119. ugly_bits = bad_bits &
  2120. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
  2121. if (ugly_bits) {
  2122. dev_warn(&spi->dev,
  2123. "setup: ignoring unsupported mode bits %x\n",
  2124. ugly_bits);
  2125. spi->mode &= ~ugly_bits;
  2126. bad_bits &= ~ugly_bits;
  2127. }
  2128. if (bad_bits) {
  2129. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  2130. bad_bits);
  2131. return -EINVAL;
  2132. }
  2133. if (!spi->bits_per_word)
  2134. spi->bits_per_word = 8;
  2135. status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word);
  2136. if (status)
  2137. return status;
  2138. if (!spi->max_speed_hz)
  2139. spi->max_speed_hz = spi->master->max_speed_hz;
  2140. if (spi->master->setup)
  2141. status = spi->master->setup(spi);
  2142. spi_set_cs(spi, false);
  2143. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  2144. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  2145. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  2146. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  2147. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  2148. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  2149. spi->bits_per_word, spi->max_speed_hz,
  2150. status);
  2151. return status;
  2152. }
  2153. EXPORT_SYMBOL_GPL(spi_setup);
  2154. static int __spi_validate(struct spi_device *spi, struct spi_message *message)
  2155. {
  2156. struct spi_master *master = spi->master;
  2157. struct spi_transfer *xfer;
  2158. int w_size;
  2159. if (list_empty(&message->transfers))
  2160. return -EINVAL;
  2161. /* Half-duplex links include original MicroWire, and ones with
  2162. * only one data pin like SPI_3WIRE (switches direction) or where
  2163. * either MOSI or MISO is missing. They can also be caused by
  2164. * software limitations.
  2165. */
  2166. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  2167. || (spi->mode & SPI_3WIRE)) {
  2168. unsigned flags = master->flags;
  2169. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  2170. if (xfer->rx_buf && xfer->tx_buf)
  2171. return -EINVAL;
  2172. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  2173. return -EINVAL;
  2174. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  2175. return -EINVAL;
  2176. }
  2177. }
  2178. /**
  2179. * Set transfer bits_per_word and max speed as spi device default if
  2180. * it is not set for this transfer.
  2181. * Set transfer tx_nbits and rx_nbits as single transfer default
  2182. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  2183. */
  2184. message->frame_length = 0;
  2185. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  2186. message->frame_length += xfer->len;
  2187. if (!xfer->bits_per_word)
  2188. xfer->bits_per_word = spi->bits_per_word;
  2189. if (!xfer->speed_hz)
  2190. xfer->speed_hz = spi->max_speed_hz;
  2191. if (!xfer->speed_hz)
  2192. xfer->speed_hz = master->max_speed_hz;
  2193. if (master->max_speed_hz &&
  2194. xfer->speed_hz > master->max_speed_hz)
  2195. xfer->speed_hz = master->max_speed_hz;
  2196. if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
  2197. return -EINVAL;
  2198. /*
  2199. * SPI transfer length should be multiple of SPI word size
  2200. * where SPI word size should be power-of-two multiple
  2201. */
  2202. if (xfer->bits_per_word <= 8)
  2203. w_size = 1;
  2204. else if (xfer->bits_per_word <= 16)
  2205. w_size = 2;
  2206. else
  2207. w_size = 4;
  2208. /* No partial transfers accepted */
  2209. if (xfer->len % w_size)
  2210. return -EINVAL;
  2211. if (xfer->speed_hz && master->min_speed_hz &&
  2212. xfer->speed_hz < master->min_speed_hz)
  2213. return -EINVAL;
  2214. if (xfer->tx_buf && !xfer->tx_nbits)
  2215. xfer->tx_nbits = SPI_NBITS_SINGLE;
  2216. if (xfer->rx_buf && !xfer->rx_nbits)
  2217. xfer->rx_nbits = SPI_NBITS_SINGLE;
  2218. /* check transfer tx/rx_nbits:
  2219. * 1. check the value matches one of single, dual and quad
  2220. * 2. check tx/rx_nbits match the mode in spi_device
  2221. */
  2222. if (xfer->tx_buf) {
  2223. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  2224. xfer->tx_nbits != SPI_NBITS_DUAL &&
  2225. xfer->tx_nbits != SPI_NBITS_QUAD)
  2226. return -EINVAL;
  2227. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  2228. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  2229. return -EINVAL;
  2230. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  2231. !(spi->mode & SPI_TX_QUAD))
  2232. return -EINVAL;
  2233. }
  2234. /* check transfer rx_nbits */
  2235. if (xfer->rx_buf) {
  2236. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  2237. xfer->rx_nbits != SPI_NBITS_DUAL &&
  2238. xfer->rx_nbits != SPI_NBITS_QUAD)
  2239. return -EINVAL;
  2240. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  2241. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  2242. return -EINVAL;
  2243. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  2244. !(spi->mode & SPI_RX_QUAD))
  2245. return -EINVAL;
  2246. }
  2247. }
  2248. message->status = -EINPROGRESS;
  2249. return 0;
  2250. }
  2251. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  2252. {
  2253. struct spi_master *master = spi->master;
  2254. message->spi = spi;
  2255. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
  2256. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
  2257. trace_spi_message_submit(message);
  2258. return master->transfer(spi, message);
  2259. }
  2260. /**
  2261. * spi_async - asynchronous SPI transfer
  2262. * @spi: device with which data will be exchanged
  2263. * @message: describes the data transfers, including completion callback
  2264. * Context: any (irqs may be blocked, etc)
  2265. *
  2266. * This call may be used in_irq and other contexts which can't sleep,
  2267. * as well as from task contexts which can sleep.
  2268. *
  2269. * The completion callback is invoked in a context which can't sleep.
  2270. * Before that invocation, the value of message->status is undefined.
  2271. * When the callback is issued, message->status holds either zero (to
  2272. * indicate complete success) or a negative error code. After that
  2273. * callback returns, the driver which issued the transfer request may
  2274. * deallocate the associated memory; it's no longer in use by any SPI
  2275. * core or controller driver code.
  2276. *
  2277. * Note that although all messages to a spi_device are handled in
  2278. * FIFO order, messages may go to different devices in other orders.
  2279. * Some device might be higher priority, or have various "hard" access
  2280. * time requirements, for example.
  2281. *
  2282. * On detection of any fault during the transfer, processing of
  2283. * the entire message is aborted, and the device is deselected.
  2284. * Until returning from the associated message completion callback,
  2285. * no other spi_message queued to that device will be processed.
  2286. * (This rule applies equally to all the synchronous transfer calls,
  2287. * which are wrappers around this core asynchronous primitive.)
  2288. *
  2289. * Return: zero on success, else a negative error code.
  2290. */
  2291. int spi_async(struct spi_device *spi, struct spi_message *message)
  2292. {
  2293. struct spi_master *master = spi->master;
  2294. int ret;
  2295. unsigned long flags;
  2296. ret = __spi_validate(spi, message);
  2297. if (ret != 0)
  2298. return ret;
  2299. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2300. if (master->bus_lock_flag)
  2301. ret = -EBUSY;
  2302. else
  2303. ret = __spi_async(spi, message);
  2304. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2305. return ret;
  2306. }
  2307. EXPORT_SYMBOL_GPL(spi_async);
  2308. /**
  2309. * spi_async_locked - version of spi_async with exclusive bus usage
  2310. * @spi: device with which data will be exchanged
  2311. * @message: describes the data transfers, including completion callback
  2312. * Context: any (irqs may be blocked, etc)
  2313. *
  2314. * This call may be used in_irq and other contexts which can't sleep,
  2315. * as well as from task contexts which can sleep.
  2316. *
  2317. * The completion callback is invoked in a context which can't sleep.
  2318. * Before that invocation, the value of message->status is undefined.
  2319. * When the callback is issued, message->status holds either zero (to
  2320. * indicate complete success) or a negative error code. After that
  2321. * callback returns, the driver which issued the transfer request may
  2322. * deallocate the associated memory; it's no longer in use by any SPI
  2323. * core or controller driver code.
  2324. *
  2325. * Note that although all messages to a spi_device are handled in
  2326. * FIFO order, messages may go to different devices in other orders.
  2327. * Some device might be higher priority, or have various "hard" access
  2328. * time requirements, for example.
  2329. *
  2330. * On detection of any fault during the transfer, processing of
  2331. * the entire message is aborted, and the device is deselected.
  2332. * Until returning from the associated message completion callback,
  2333. * no other spi_message queued to that device will be processed.
  2334. * (This rule applies equally to all the synchronous transfer calls,
  2335. * which are wrappers around this core asynchronous primitive.)
  2336. *
  2337. * Return: zero on success, else a negative error code.
  2338. */
  2339. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  2340. {
  2341. struct spi_master *master = spi->master;
  2342. int ret;
  2343. unsigned long flags;
  2344. ret = __spi_validate(spi, message);
  2345. if (ret != 0)
  2346. return ret;
  2347. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2348. ret = __spi_async(spi, message);
  2349. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2350. return ret;
  2351. }
  2352. EXPORT_SYMBOL_GPL(spi_async_locked);
  2353. int spi_flash_read(struct spi_device *spi,
  2354. struct spi_flash_read_message *msg)
  2355. {
  2356. struct spi_master *master = spi->master;
  2357. struct device *rx_dev = NULL;
  2358. int ret;
  2359. if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
  2360. msg->addr_nbits == SPI_NBITS_DUAL) &&
  2361. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  2362. return -EINVAL;
  2363. if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
  2364. msg->addr_nbits == SPI_NBITS_QUAD) &&
  2365. !(spi->mode & SPI_TX_QUAD))
  2366. return -EINVAL;
  2367. if (msg->data_nbits == SPI_NBITS_DUAL &&
  2368. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  2369. return -EINVAL;
  2370. if (msg->data_nbits == SPI_NBITS_QUAD &&
  2371. !(spi->mode & SPI_RX_QUAD))
  2372. return -EINVAL;
  2373. if (master->auto_runtime_pm) {
  2374. ret = pm_runtime_get_sync(master->dev.parent);
  2375. if (ret < 0) {
  2376. dev_err(&master->dev, "Failed to power device: %d\n",
  2377. ret);
  2378. return ret;
  2379. }
  2380. }
  2381. mutex_lock(&master->bus_lock_mutex);
  2382. mutex_lock(&master->io_mutex);
  2383. if (master->dma_rx) {
  2384. rx_dev = master->dma_rx->device->dev;
  2385. if (virt_addr_valid(msg->buf)) {
  2386. ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
  2387. msg->buf, msg->len,
  2388. DMA_FROM_DEVICE);
  2389. if (!ret)
  2390. msg->cur_msg_mapped = true;
  2391. }
  2392. }
  2393. ret = master->spi_flash_read(spi, msg);
  2394. if (msg->cur_msg_mapped)
  2395. spi_unmap_buf(master, rx_dev, &msg->rx_sg,
  2396. DMA_FROM_DEVICE);
  2397. mutex_unlock(&master->io_mutex);
  2398. mutex_unlock(&master->bus_lock_mutex);
  2399. if (master->auto_runtime_pm)
  2400. pm_runtime_put(master->dev.parent);
  2401. return ret;
  2402. }
  2403. EXPORT_SYMBOL_GPL(spi_flash_read);
  2404. /*-------------------------------------------------------------------------*/
  2405. /* Utility methods for SPI master protocol drivers, layered on
  2406. * top of the core. Some other utility methods are defined as
  2407. * inline functions.
  2408. */
  2409. static void spi_complete(void *arg)
  2410. {
  2411. complete(arg);
  2412. }
  2413. static int __spi_sync(struct spi_device *spi, struct spi_message *message)
  2414. {
  2415. DECLARE_COMPLETION_ONSTACK(done);
  2416. int status;
  2417. struct spi_master *master = spi->master;
  2418. unsigned long flags;
  2419. status = __spi_validate(spi, message);
  2420. if (status != 0)
  2421. return status;
  2422. message->complete = spi_complete;
  2423. message->context = &done;
  2424. message->spi = spi;
  2425. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
  2426. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
  2427. /* If we're not using the legacy transfer method then we will
  2428. * try to transfer in the calling context so special case.
  2429. * This code would be less tricky if we could remove the
  2430. * support for driver implemented message queues.
  2431. */
  2432. if (master->transfer == spi_queued_transfer) {
  2433. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2434. trace_spi_message_submit(message);
  2435. status = __spi_queued_transfer(spi, message, false);
  2436. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2437. } else {
  2438. status = spi_async_locked(spi, message);
  2439. }
  2440. if (status == 0) {
  2441. /* Push out the messages in the calling context if we
  2442. * can.
  2443. */
  2444. if (master->transfer == spi_queued_transfer) {
  2445. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
  2446. spi_sync_immediate);
  2447. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
  2448. spi_sync_immediate);
  2449. __spi_pump_messages(master, false);
  2450. }
  2451. wait_for_completion(&done);
  2452. status = message->status;
  2453. }
  2454. message->context = NULL;
  2455. return status;
  2456. }
  2457. /**
  2458. * spi_sync - blocking/synchronous SPI data transfers
  2459. * @spi: device with which data will be exchanged
  2460. * @message: describes the data transfers
  2461. * Context: can sleep
  2462. *
  2463. * This call may only be used from a context that may sleep. The sleep
  2464. * is non-interruptible, and has no timeout. Low-overhead controller
  2465. * drivers may DMA directly into and out of the message buffers.
  2466. *
  2467. * Note that the SPI device's chip select is active during the message,
  2468. * and then is normally disabled between messages. Drivers for some
  2469. * frequently-used devices may want to minimize costs of selecting a chip,
  2470. * by leaving it selected in anticipation that the next message will go
  2471. * to the same chip. (That may increase power usage.)
  2472. *
  2473. * Also, the caller is guaranteeing that the memory associated with the
  2474. * message will not be freed before this call returns.
  2475. *
  2476. * Return: zero on success, else a negative error code.
  2477. */
  2478. int spi_sync(struct spi_device *spi, struct spi_message *message)
  2479. {
  2480. int ret;
  2481. mutex_lock(&spi->master->bus_lock_mutex);
  2482. ret = __spi_sync(spi, message);
  2483. mutex_unlock(&spi->master->bus_lock_mutex);
  2484. return ret;
  2485. }
  2486. EXPORT_SYMBOL_GPL(spi_sync);
  2487. /**
  2488. * spi_sync_locked - version of spi_sync with exclusive bus usage
  2489. * @spi: device with which data will be exchanged
  2490. * @message: describes the data transfers
  2491. * Context: can sleep
  2492. *
  2493. * This call may only be used from a context that may sleep. The sleep
  2494. * is non-interruptible, and has no timeout. Low-overhead controller
  2495. * drivers may DMA directly into and out of the message buffers.
  2496. *
  2497. * This call should be used by drivers that require exclusive access to the
  2498. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  2499. * be released by a spi_bus_unlock call when the exclusive access is over.
  2500. *
  2501. * Return: zero on success, else a negative error code.
  2502. */
  2503. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  2504. {
  2505. return __spi_sync(spi, message);
  2506. }
  2507. EXPORT_SYMBOL_GPL(spi_sync_locked);
  2508. /**
  2509. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  2510. * @master: SPI bus master that should be locked for exclusive bus access
  2511. * Context: can sleep
  2512. *
  2513. * This call may only be used from a context that may sleep. The sleep
  2514. * is non-interruptible, and has no timeout.
  2515. *
  2516. * This call should be used by drivers that require exclusive access to the
  2517. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  2518. * exclusive access is over. Data transfer must be done by spi_sync_locked
  2519. * and spi_async_locked calls when the SPI bus lock is held.
  2520. *
  2521. * Return: always zero.
  2522. */
  2523. int spi_bus_lock(struct spi_master *master)
  2524. {
  2525. unsigned long flags;
  2526. mutex_lock(&master->bus_lock_mutex);
  2527. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2528. master->bus_lock_flag = 1;
  2529. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2530. /* mutex remains locked until spi_bus_unlock is called */
  2531. return 0;
  2532. }
  2533. EXPORT_SYMBOL_GPL(spi_bus_lock);
  2534. /**
  2535. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  2536. * @master: SPI bus master that was locked for exclusive bus access
  2537. * Context: can sleep
  2538. *
  2539. * This call may only be used from a context that may sleep. The sleep
  2540. * is non-interruptible, and has no timeout.
  2541. *
  2542. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  2543. * call.
  2544. *
  2545. * Return: always zero.
  2546. */
  2547. int spi_bus_unlock(struct spi_master *master)
  2548. {
  2549. master->bus_lock_flag = 0;
  2550. mutex_unlock(&master->bus_lock_mutex);
  2551. return 0;
  2552. }
  2553. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  2554. /* portable code must never pass more than 32 bytes */
  2555. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  2556. static u8 *buf;
  2557. /**
  2558. * spi_write_then_read - SPI synchronous write followed by read
  2559. * @spi: device with which data will be exchanged
  2560. * @txbuf: data to be written (need not be dma-safe)
  2561. * @n_tx: size of txbuf, in bytes
  2562. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  2563. * @n_rx: size of rxbuf, in bytes
  2564. * Context: can sleep
  2565. *
  2566. * This performs a half duplex MicroWire style transaction with the
  2567. * device, sending txbuf and then reading rxbuf. The return value
  2568. * is zero for success, else a negative errno status code.
  2569. * This call may only be used from a context that may sleep.
  2570. *
  2571. * Parameters to this routine are always copied using a small buffer;
  2572. * portable code should never use this for more than 32 bytes.
  2573. * Performance-sensitive or bulk transfer code should instead use
  2574. * spi_{async,sync}() calls with dma-safe buffers.
  2575. *
  2576. * Return: zero on success, else a negative error code.
  2577. */
  2578. int spi_write_then_read(struct spi_device *spi,
  2579. const void *txbuf, unsigned n_tx,
  2580. void *rxbuf, unsigned n_rx)
  2581. {
  2582. static DEFINE_MUTEX(lock);
  2583. int status;
  2584. struct spi_message message;
  2585. struct spi_transfer x[2];
  2586. u8 *local_buf;
  2587. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  2588. * copying here, (as a pure convenience thing), but we can
  2589. * keep heap costs out of the hot path unless someone else is
  2590. * using the pre-allocated buffer or the transfer is too large.
  2591. */
  2592. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  2593. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  2594. GFP_KERNEL | GFP_DMA);
  2595. if (!local_buf)
  2596. return -ENOMEM;
  2597. } else {
  2598. local_buf = buf;
  2599. }
  2600. spi_message_init(&message);
  2601. memset(x, 0, sizeof(x));
  2602. if (n_tx) {
  2603. x[0].len = n_tx;
  2604. spi_message_add_tail(&x[0], &message);
  2605. }
  2606. if (n_rx) {
  2607. x[1].len = n_rx;
  2608. spi_message_add_tail(&x[1], &message);
  2609. }
  2610. memcpy(local_buf, txbuf, n_tx);
  2611. x[0].tx_buf = local_buf;
  2612. x[1].rx_buf = local_buf + n_tx;
  2613. /* do the i/o */
  2614. status = spi_sync(spi, &message);
  2615. if (status == 0)
  2616. memcpy(rxbuf, x[1].rx_buf, n_rx);
  2617. if (x[0].tx_buf == buf)
  2618. mutex_unlock(&lock);
  2619. else
  2620. kfree(local_buf);
  2621. return status;
  2622. }
  2623. EXPORT_SYMBOL_GPL(spi_write_then_read);
  2624. /*-------------------------------------------------------------------------*/
  2625. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  2626. static int __spi_of_device_match(struct device *dev, void *data)
  2627. {
  2628. return dev->of_node == data;
  2629. }
  2630. /* must call put_device() when done with returned spi_device device */
  2631. static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
  2632. {
  2633. struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
  2634. __spi_of_device_match);
  2635. return dev ? to_spi_device(dev) : NULL;
  2636. }
  2637. static int __spi_of_master_match(struct device *dev, const void *data)
  2638. {
  2639. return dev->of_node == data;
  2640. }
  2641. /* the spi masters are not using spi_bus, so we find it with another way */
  2642. static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
  2643. {
  2644. struct device *dev;
  2645. dev = class_find_device(&spi_master_class, NULL, node,
  2646. __spi_of_master_match);
  2647. if (!dev)
  2648. return NULL;
  2649. /* reference got in class_find_device */
  2650. return container_of(dev, struct spi_master, dev);
  2651. }
  2652. static int of_spi_notify(struct notifier_block *nb, unsigned long action,
  2653. void *arg)
  2654. {
  2655. struct of_reconfig_data *rd = arg;
  2656. struct spi_master *master;
  2657. struct spi_device *spi;
  2658. switch (of_reconfig_get_state_change(action, arg)) {
  2659. case OF_RECONFIG_CHANGE_ADD:
  2660. master = of_find_spi_master_by_node(rd->dn->parent);
  2661. if (master == NULL)
  2662. return NOTIFY_OK; /* not for us */
  2663. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  2664. put_device(&master->dev);
  2665. return NOTIFY_OK;
  2666. }
  2667. spi = of_register_spi_device(master, rd->dn);
  2668. put_device(&master->dev);
  2669. if (IS_ERR(spi)) {
  2670. pr_err("%s: failed to create for '%s'\n",
  2671. __func__, rd->dn->full_name);
  2672. of_node_clear_flag(rd->dn, OF_POPULATED);
  2673. return notifier_from_errno(PTR_ERR(spi));
  2674. }
  2675. break;
  2676. case OF_RECONFIG_CHANGE_REMOVE:
  2677. /* already depopulated? */
  2678. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  2679. return NOTIFY_OK;
  2680. /* find our device by node */
  2681. spi = of_find_spi_device_by_node(rd->dn);
  2682. if (spi == NULL)
  2683. return NOTIFY_OK; /* no? not meant for us */
  2684. /* unregister takes one ref away */
  2685. spi_unregister_device(spi);
  2686. /* and put the reference of the find */
  2687. put_device(&spi->dev);
  2688. break;
  2689. }
  2690. return NOTIFY_OK;
  2691. }
  2692. static struct notifier_block spi_of_notifier = {
  2693. .notifier_call = of_spi_notify,
  2694. };
  2695. #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2696. extern struct notifier_block spi_of_notifier;
  2697. #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2698. #if IS_ENABLED(CONFIG_ACPI)
  2699. static int spi_acpi_master_match(struct device *dev, const void *data)
  2700. {
  2701. return ACPI_COMPANION(dev->parent) == data;
  2702. }
  2703. static int spi_acpi_device_match(struct device *dev, void *data)
  2704. {
  2705. return ACPI_COMPANION(dev) == data;
  2706. }
  2707. static struct spi_master *acpi_spi_find_master_by_adev(struct acpi_device *adev)
  2708. {
  2709. struct device *dev;
  2710. dev = class_find_device(&spi_master_class, NULL, adev,
  2711. spi_acpi_master_match);
  2712. if (!dev)
  2713. return NULL;
  2714. return container_of(dev, struct spi_master, dev);
  2715. }
  2716. static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
  2717. {
  2718. struct device *dev;
  2719. dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
  2720. return dev ? to_spi_device(dev) : NULL;
  2721. }
  2722. static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
  2723. void *arg)
  2724. {
  2725. struct acpi_device *adev = arg;
  2726. struct spi_master *master;
  2727. struct spi_device *spi;
  2728. switch (value) {
  2729. case ACPI_RECONFIG_DEVICE_ADD:
  2730. master = acpi_spi_find_master_by_adev(adev->parent);
  2731. if (!master)
  2732. break;
  2733. acpi_register_spi_device(master, adev);
  2734. put_device(&master->dev);
  2735. break;
  2736. case ACPI_RECONFIG_DEVICE_REMOVE:
  2737. if (!acpi_device_enumerated(adev))
  2738. break;
  2739. spi = acpi_spi_find_device_by_adev(adev);
  2740. if (!spi)
  2741. break;
  2742. spi_unregister_device(spi);
  2743. put_device(&spi->dev);
  2744. break;
  2745. }
  2746. return NOTIFY_OK;
  2747. }
  2748. static struct notifier_block spi_acpi_notifier = {
  2749. .notifier_call = acpi_spi_notify,
  2750. };
  2751. #else
  2752. extern struct notifier_block spi_acpi_notifier;
  2753. #endif
  2754. static int __init spi_init(void)
  2755. {
  2756. int status;
  2757. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  2758. if (!buf) {
  2759. status = -ENOMEM;
  2760. goto err0;
  2761. }
  2762. status = bus_register(&spi_bus_type);
  2763. if (status < 0)
  2764. goto err1;
  2765. status = class_register(&spi_master_class);
  2766. if (status < 0)
  2767. goto err2;
  2768. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  2769. WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
  2770. if (IS_ENABLED(CONFIG_ACPI))
  2771. WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
  2772. return 0;
  2773. err2:
  2774. bus_unregister(&spi_bus_type);
  2775. err1:
  2776. kfree(buf);
  2777. buf = NULL;
  2778. err0:
  2779. return status;
  2780. }
  2781. /* board_info is normally registered in arch_initcall(),
  2782. * but even essential drivers wait till later
  2783. *
  2784. * REVISIT only boardinfo really needs static linking. the rest (device and
  2785. * driver registration) _could_ be dynamically linked (modular) ... costs
  2786. * include needing to have boardinfo data structures be much more public.
  2787. */
  2788. postcore_initcall(spi_init);