spi.c 88 KB

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