spi.c 95 KB

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