spi.c 94 KB

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