regmap.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042
  1. /*
  2. * Register map access API
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/export.h>
  15. #include <linux/mutex.h>
  16. #include <linux/err.h>
  17. #include <linux/of.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/sched.h>
  20. #include <linux/delay.h>
  21. #include <linux/log2.h>
  22. #include <linux/hwspinlock.h>
  23. #define CREATE_TRACE_POINTS
  24. #include "trace.h"
  25. #include "internal.h"
  26. /*
  27. * Sometimes for failures during very early init the trace
  28. * infrastructure isn't available early enough to be used. For this
  29. * sort of problem defining LOG_DEVICE will add printks for basic
  30. * register I/O on a specific device.
  31. */
  32. #undef LOG_DEVICE
  33. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  34. unsigned int mask, unsigned int val,
  35. bool *change, bool force_write);
  36. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  37. unsigned int *val);
  38. static int _regmap_bus_read(void *context, unsigned int reg,
  39. unsigned int *val);
  40. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  41. unsigned int val);
  42. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  43. unsigned int val);
  44. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  45. unsigned int val);
  46. bool regmap_reg_in_ranges(unsigned int reg,
  47. const struct regmap_range *ranges,
  48. unsigned int nranges)
  49. {
  50. const struct regmap_range *r;
  51. int i;
  52. for (i = 0, r = ranges; i < nranges; i++, r++)
  53. if (regmap_reg_in_range(reg, r))
  54. return true;
  55. return false;
  56. }
  57. EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
  58. bool regmap_check_range_table(struct regmap *map, unsigned int reg,
  59. const struct regmap_access_table *table)
  60. {
  61. /* Check "no ranges" first */
  62. if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
  63. return false;
  64. /* In case zero "yes ranges" are supplied, any reg is OK */
  65. if (!table->n_yes_ranges)
  66. return true;
  67. return regmap_reg_in_ranges(reg, table->yes_ranges,
  68. table->n_yes_ranges);
  69. }
  70. EXPORT_SYMBOL_GPL(regmap_check_range_table);
  71. bool regmap_writeable(struct regmap *map, unsigned int reg)
  72. {
  73. if (map->max_register && reg > map->max_register)
  74. return false;
  75. if (map->writeable_reg)
  76. return map->writeable_reg(map->dev, reg);
  77. if (map->wr_table)
  78. return regmap_check_range_table(map, reg, map->wr_table);
  79. return true;
  80. }
  81. bool regmap_cached(struct regmap *map, unsigned int reg)
  82. {
  83. int ret;
  84. unsigned int val;
  85. if (map->cache_type == REGCACHE_NONE)
  86. return false;
  87. if (!map->cache_ops)
  88. return false;
  89. if (map->max_register && reg > map->max_register)
  90. return false;
  91. map->lock(map->lock_arg);
  92. ret = regcache_read(map, reg, &val);
  93. map->unlock(map->lock_arg);
  94. if (ret)
  95. return false;
  96. return true;
  97. }
  98. bool regmap_readable(struct regmap *map, unsigned int reg)
  99. {
  100. if (!map->reg_read)
  101. return false;
  102. if (map->max_register && reg > map->max_register)
  103. return false;
  104. if (map->format.format_write)
  105. return false;
  106. if (map->readable_reg)
  107. return map->readable_reg(map->dev, reg);
  108. if (map->rd_table)
  109. return regmap_check_range_table(map, reg, map->rd_table);
  110. return true;
  111. }
  112. bool regmap_volatile(struct regmap *map, unsigned int reg)
  113. {
  114. if (!map->format.format_write && !regmap_readable(map, reg))
  115. return false;
  116. if (map->volatile_reg)
  117. return map->volatile_reg(map->dev, reg);
  118. if (map->volatile_table)
  119. return regmap_check_range_table(map, reg, map->volatile_table);
  120. if (map->cache_ops)
  121. return false;
  122. else
  123. return true;
  124. }
  125. bool regmap_precious(struct regmap *map, unsigned int reg)
  126. {
  127. if (!regmap_readable(map, reg))
  128. return false;
  129. if (map->precious_reg)
  130. return map->precious_reg(map->dev, reg);
  131. if (map->precious_table)
  132. return regmap_check_range_table(map, reg, map->precious_table);
  133. return false;
  134. }
  135. bool regmap_readable_noinc(struct regmap *map, unsigned int reg)
  136. {
  137. if (map->readable_noinc_reg)
  138. return map->readable_noinc_reg(map->dev, reg);
  139. if (map->rd_noinc_table)
  140. return regmap_check_range_table(map, reg, map->rd_noinc_table);
  141. return true;
  142. }
  143. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  144. size_t num)
  145. {
  146. unsigned int i;
  147. for (i = 0; i < num; i++)
  148. if (!regmap_volatile(map, reg + regmap_get_offset(map, i)))
  149. return false;
  150. return true;
  151. }
  152. static void regmap_format_2_6_write(struct regmap *map,
  153. unsigned int reg, unsigned int val)
  154. {
  155. u8 *out = map->work_buf;
  156. *out = (reg << 6) | val;
  157. }
  158. static void regmap_format_4_12_write(struct regmap *map,
  159. unsigned int reg, unsigned int val)
  160. {
  161. __be16 *out = map->work_buf;
  162. *out = cpu_to_be16((reg << 12) | val);
  163. }
  164. static void regmap_format_7_9_write(struct regmap *map,
  165. unsigned int reg, unsigned int val)
  166. {
  167. __be16 *out = map->work_buf;
  168. *out = cpu_to_be16((reg << 9) | val);
  169. }
  170. static void regmap_format_10_14_write(struct regmap *map,
  171. unsigned int reg, unsigned int val)
  172. {
  173. u8 *out = map->work_buf;
  174. out[2] = val;
  175. out[1] = (val >> 8) | (reg << 6);
  176. out[0] = reg >> 2;
  177. }
  178. static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
  179. {
  180. u8 *b = buf;
  181. b[0] = val << shift;
  182. }
  183. static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
  184. {
  185. __be16 *b = buf;
  186. b[0] = cpu_to_be16(val << shift);
  187. }
  188. static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
  189. {
  190. __le16 *b = buf;
  191. b[0] = cpu_to_le16(val << shift);
  192. }
  193. static void regmap_format_16_native(void *buf, unsigned int val,
  194. unsigned int shift)
  195. {
  196. *(u16 *)buf = val << shift;
  197. }
  198. static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
  199. {
  200. u8 *b = buf;
  201. val <<= shift;
  202. b[0] = val >> 16;
  203. b[1] = val >> 8;
  204. b[2] = val;
  205. }
  206. static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
  207. {
  208. __be32 *b = buf;
  209. b[0] = cpu_to_be32(val << shift);
  210. }
  211. static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
  212. {
  213. __le32 *b = buf;
  214. b[0] = cpu_to_le32(val << shift);
  215. }
  216. static void regmap_format_32_native(void *buf, unsigned int val,
  217. unsigned int shift)
  218. {
  219. *(u32 *)buf = val << shift;
  220. }
  221. #ifdef CONFIG_64BIT
  222. static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
  223. {
  224. __be64 *b = buf;
  225. b[0] = cpu_to_be64((u64)val << shift);
  226. }
  227. static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
  228. {
  229. __le64 *b = buf;
  230. b[0] = cpu_to_le64((u64)val << shift);
  231. }
  232. static void regmap_format_64_native(void *buf, unsigned int val,
  233. unsigned int shift)
  234. {
  235. *(u64 *)buf = (u64)val << shift;
  236. }
  237. #endif
  238. static void regmap_parse_inplace_noop(void *buf)
  239. {
  240. }
  241. static unsigned int regmap_parse_8(const void *buf)
  242. {
  243. const u8 *b = buf;
  244. return b[0];
  245. }
  246. static unsigned int regmap_parse_16_be(const void *buf)
  247. {
  248. const __be16 *b = buf;
  249. return be16_to_cpu(b[0]);
  250. }
  251. static unsigned int regmap_parse_16_le(const void *buf)
  252. {
  253. const __le16 *b = buf;
  254. return le16_to_cpu(b[0]);
  255. }
  256. static void regmap_parse_16_be_inplace(void *buf)
  257. {
  258. __be16 *b = buf;
  259. b[0] = be16_to_cpu(b[0]);
  260. }
  261. static void regmap_parse_16_le_inplace(void *buf)
  262. {
  263. __le16 *b = buf;
  264. b[0] = le16_to_cpu(b[0]);
  265. }
  266. static unsigned int regmap_parse_16_native(const void *buf)
  267. {
  268. return *(u16 *)buf;
  269. }
  270. static unsigned int regmap_parse_24(const void *buf)
  271. {
  272. const u8 *b = buf;
  273. unsigned int ret = b[2];
  274. ret |= ((unsigned int)b[1]) << 8;
  275. ret |= ((unsigned int)b[0]) << 16;
  276. return ret;
  277. }
  278. static unsigned int regmap_parse_32_be(const void *buf)
  279. {
  280. const __be32 *b = buf;
  281. return be32_to_cpu(b[0]);
  282. }
  283. static unsigned int regmap_parse_32_le(const void *buf)
  284. {
  285. const __le32 *b = buf;
  286. return le32_to_cpu(b[0]);
  287. }
  288. static void regmap_parse_32_be_inplace(void *buf)
  289. {
  290. __be32 *b = buf;
  291. b[0] = be32_to_cpu(b[0]);
  292. }
  293. static void regmap_parse_32_le_inplace(void *buf)
  294. {
  295. __le32 *b = buf;
  296. b[0] = le32_to_cpu(b[0]);
  297. }
  298. static unsigned int regmap_parse_32_native(const void *buf)
  299. {
  300. return *(u32 *)buf;
  301. }
  302. #ifdef CONFIG_64BIT
  303. static unsigned int regmap_parse_64_be(const void *buf)
  304. {
  305. const __be64 *b = buf;
  306. return be64_to_cpu(b[0]);
  307. }
  308. static unsigned int regmap_parse_64_le(const void *buf)
  309. {
  310. const __le64 *b = buf;
  311. return le64_to_cpu(b[0]);
  312. }
  313. static void regmap_parse_64_be_inplace(void *buf)
  314. {
  315. __be64 *b = buf;
  316. b[0] = be64_to_cpu(b[0]);
  317. }
  318. static void regmap_parse_64_le_inplace(void *buf)
  319. {
  320. __le64 *b = buf;
  321. b[0] = le64_to_cpu(b[0]);
  322. }
  323. static unsigned int regmap_parse_64_native(const void *buf)
  324. {
  325. return *(u64 *)buf;
  326. }
  327. #endif
  328. static void regmap_lock_hwlock(void *__map)
  329. {
  330. struct regmap *map = __map;
  331. hwspin_lock_timeout(map->hwlock, UINT_MAX);
  332. }
  333. static void regmap_lock_hwlock_irq(void *__map)
  334. {
  335. struct regmap *map = __map;
  336. hwspin_lock_timeout_irq(map->hwlock, UINT_MAX);
  337. }
  338. static void regmap_lock_hwlock_irqsave(void *__map)
  339. {
  340. struct regmap *map = __map;
  341. hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
  342. &map->spinlock_flags);
  343. }
  344. static void regmap_unlock_hwlock(void *__map)
  345. {
  346. struct regmap *map = __map;
  347. hwspin_unlock(map->hwlock);
  348. }
  349. static void regmap_unlock_hwlock_irq(void *__map)
  350. {
  351. struct regmap *map = __map;
  352. hwspin_unlock_irq(map->hwlock);
  353. }
  354. static void regmap_unlock_hwlock_irqrestore(void *__map)
  355. {
  356. struct regmap *map = __map;
  357. hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags);
  358. }
  359. static void regmap_lock_unlock_none(void *__map)
  360. {
  361. }
  362. static void regmap_lock_mutex(void *__map)
  363. {
  364. struct regmap *map = __map;
  365. mutex_lock(&map->mutex);
  366. }
  367. static void regmap_unlock_mutex(void *__map)
  368. {
  369. struct regmap *map = __map;
  370. mutex_unlock(&map->mutex);
  371. }
  372. static void regmap_lock_spinlock(void *__map)
  373. __acquires(&map->spinlock)
  374. {
  375. struct regmap *map = __map;
  376. unsigned long flags;
  377. spin_lock_irqsave(&map->spinlock, flags);
  378. map->spinlock_flags = flags;
  379. }
  380. static void regmap_unlock_spinlock(void *__map)
  381. __releases(&map->spinlock)
  382. {
  383. struct regmap *map = __map;
  384. spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
  385. }
  386. static void dev_get_regmap_release(struct device *dev, void *res)
  387. {
  388. /*
  389. * We don't actually have anything to do here; the goal here
  390. * is not to manage the regmap but to provide a simple way to
  391. * get the regmap back given a struct device.
  392. */
  393. }
  394. static bool _regmap_range_add(struct regmap *map,
  395. struct regmap_range_node *data)
  396. {
  397. struct rb_root *root = &map->range_tree;
  398. struct rb_node **new = &(root->rb_node), *parent = NULL;
  399. while (*new) {
  400. struct regmap_range_node *this =
  401. rb_entry(*new, struct regmap_range_node, node);
  402. parent = *new;
  403. if (data->range_max < this->range_min)
  404. new = &((*new)->rb_left);
  405. else if (data->range_min > this->range_max)
  406. new = &((*new)->rb_right);
  407. else
  408. return false;
  409. }
  410. rb_link_node(&data->node, parent, new);
  411. rb_insert_color(&data->node, root);
  412. return true;
  413. }
  414. static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
  415. unsigned int reg)
  416. {
  417. struct rb_node *node = map->range_tree.rb_node;
  418. while (node) {
  419. struct regmap_range_node *this =
  420. rb_entry(node, struct regmap_range_node, node);
  421. if (reg < this->range_min)
  422. node = node->rb_left;
  423. else if (reg > this->range_max)
  424. node = node->rb_right;
  425. else
  426. return this;
  427. }
  428. return NULL;
  429. }
  430. static void regmap_range_exit(struct regmap *map)
  431. {
  432. struct rb_node *next;
  433. struct regmap_range_node *range_node;
  434. next = rb_first(&map->range_tree);
  435. while (next) {
  436. range_node = rb_entry(next, struct regmap_range_node, node);
  437. next = rb_next(&range_node->node);
  438. rb_erase(&range_node->node, &map->range_tree);
  439. kfree(range_node);
  440. }
  441. kfree(map->selector_work_buf);
  442. }
  443. int regmap_attach_dev(struct device *dev, struct regmap *map,
  444. const struct regmap_config *config)
  445. {
  446. struct regmap **m;
  447. map->dev = dev;
  448. regmap_debugfs_init(map, config->name);
  449. /* Add a devres resource for dev_get_regmap() */
  450. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  451. if (!m) {
  452. regmap_debugfs_exit(map);
  453. return -ENOMEM;
  454. }
  455. *m = map;
  456. devres_add(dev, m);
  457. return 0;
  458. }
  459. EXPORT_SYMBOL_GPL(regmap_attach_dev);
  460. static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
  461. const struct regmap_config *config)
  462. {
  463. enum regmap_endian endian;
  464. /* Retrieve the endianness specification from the regmap config */
  465. endian = config->reg_format_endian;
  466. /* If the regmap config specified a non-default value, use that */
  467. if (endian != REGMAP_ENDIAN_DEFAULT)
  468. return endian;
  469. /* Retrieve the endianness specification from the bus config */
  470. if (bus && bus->reg_format_endian_default)
  471. endian = bus->reg_format_endian_default;
  472. /* If the bus specified a non-default value, use that */
  473. if (endian != REGMAP_ENDIAN_DEFAULT)
  474. return endian;
  475. /* Use this if no other value was found */
  476. return REGMAP_ENDIAN_BIG;
  477. }
  478. enum regmap_endian regmap_get_val_endian(struct device *dev,
  479. const struct regmap_bus *bus,
  480. const struct regmap_config *config)
  481. {
  482. struct device_node *np;
  483. enum regmap_endian endian;
  484. /* Retrieve the endianness specification from the regmap config */
  485. endian = config->val_format_endian;
  486. /* If the regmap config specified a non-default value, use that */
  487. if (endian != REGMAP_ENDIAN_DEFAULT)
  488. return endian;
  489. /* If the dev and dev->of_node exist try to get endianness from DT */
  490. if (dev && dev->of_node) {
  491. np = dev->of_node;
  492. /* Parse the device's DT node for an endianness specification */
  493. if (of_property_read_bool(np, "big-endian"))
  494. endian = REGMAP_ENDIAN_BIG;
  495. else if (of_property_read_bool(np, "little-endian"))
  496. endian = REGMAP_ENDIAN_LITTLE;
  497. else if (of_property_read_bool(np, "native-endian"))
  498. endian = REGMAP_ENDIAN_NATIVE;
  499. /* If the endianness was specified in DT, use that */
  500. if (endian != REGMAP_ENDIAN_DEFAULT)
  501. return endian;
  502. }
  503. /* Retrieve the endianness specification from the bus config */
  504. if (bus && bus->val_format_endian_default)
  505. endian = bus->val_format_endian_default;
  506. /* If the bus specified a non-default value, use that */
  507. if (endian != REGMAP_ENDIAN_DEFAULT)
  508. return endian;
  509. /* Use this if no other value was found */
  510. return REGMAP_ENDIAN_BIG;
  511. }
  512. EXPORT_SYMBOL_GPL(regmap_get_val_endian);
  513. struct regmap *__regmap_init(struct device *dev,
  514. const struct regmap_bus *bus,
  515. void *bus_context,
  516. const struct regmap_config *config,
  517. struct lock_class_key *lock_key,
  518. const char *lock_name)
  519. {
  520. struct regmap *map;
  521. int ret = -EINVAL;
  522. enum regmap_endian reg_endian, val_endian;
  523. int i, j;
  524. if (!config)
  525. goto err;
  526. map = kzalloc(sizeof(*map), GFP_KERNEL);
  527. if (map == NULL) {
  528. ret = -ENOMEM;
  529. goto err;
  530. }
  531. if (config->name) {
  532. map->name = kstrdup_const(config->name, GFP_KERNEL);
  533. if (!map->name) {
  534. ret = -ENOMEM;
  535. goto err_map;
  536. }
  537. }
  538. if (config->disable_locking) {
  539. map->lock = map->unlock = regmap_lock_unlock_none;
  540. regmap_debugfs_disable(map);
  541. } else if (config->lock && config->unlock) {
  542. map->lock = config->lock;
  543. map->unlock = config->unlock;
  544. map->lock_arg = config->lock_arg;
  545. } else if (config->use_hwlock) {
  546. map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
  547. if (!map->hwlock) {
  548. ret = -ENXIO;
  549. goto err_name;
  550. }
  551. switch (config->hwlock_mode) {
  552. case HWLOCK_IRQSTATE:
  553. map->lock = regmap_lock_hwlock_irqsave;
  554. map->unlock = regmap_unlock_hwlock_irqrestore;
  555. break;
  556. case HWLOCK_IRQ:
  557. map->lock = regmap_lock_hwlock_irq;
  558. map->unlock = regmap_unlock_hwlock_irq;
  559. break;
  560. default:
  561. map->lock = regmap_lock_hwlock;
  562. map->unlock = regmap_unlock_hwlock;
  563. break;
  564. }
  565. map->lock_arg = map;
  566. } else {
  567. if ((bus && bus->fast_io) ||
  568. config->fast_io) {
  569. spin_lock_init(&map->spinlock);
  570. map->lock = regmap_lock_spinlock;
  571. map->unlock = regmap_unlock_spinlock;
  572. lockdep_set_class_and_name(&map->spinlock,
  573. lock_key, lock_name);
  574. } else {
  575. mutex_init(&map->mutex);
  576. map->lock = regmap_lock_mutex;
  577. map->unlock = regmap_unlock_mutex;
  578. lockdep_set_class_and_name(&map->mutex,
  579. lock_key, lock_name);
  580. }
  581. map->lock_arg = map;
  582. }
  583. /*
  584. * When we write in fast-paths with regmap_bulk_write() don't allocate
  585. * scratch buffers with sleeping allocations.
  586. */
  587. if ((bus && bus->fast_io) || config->fast_io)
  588. map->alloc_flags = GFP_ATOMIC;
  589. else
  590. map->alloc_flags = GFP_KERNEL;
  591. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  592. map->format.pad_bytes = config->pad_bits / 8;
  593. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  594. map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
  595. config->val_bits + config->pad_bits, 8);
  596. map->reg_shift = config->pad_bits % 8;
  597. if (config->reg_stride)
  598. map->reg_stride = config->reg_stride;
  599. else
  600. map->reg_stride = 1;
  601. if (is_power_of_2(map->reg_stride))
  602. map->reg_stride_order = ilog2(map->reg_stride);
  603. else
  604. map->reg_stride_order = -1;
  605. map->use_single_read = config->use_single_rw || !bus || !bus->read;
  606. map->use_single_write = config->use_single_rw || !bus || !bus->write;
  607. map->can_multi_write = config->can_multi_write && bus && bus->write;
  608. if (bus) {
  609. map->max_raw_read = bus->max_raw_read;
  610. map->max_raw_write = bus->max_raw_write;
  611. }
  612. map->dev = dev;
  613. map->bus = bus;
  614. map->bus_context = bus_context;
  615. map->max_register = config->max_register;
  616. map->wr_table = config->wr_table;
  617. map->rd_table = config->rd_table;
  618. map->volatile_table = config->volatile_table;
  619. map->precious_table = config->precious_table;
  620. map->rd_noinc_table = config->rd_noinc_table;
  621. map->writeable_reg = config->writeable_reg;
  622. map->readable_reg = config->readable_reg;
  623. map->volatile_reg = config->volatile_reg;
  624. map->precious_reg = config->precious_reg;
  625. map->readable_noinc_reg = config->readable_noinc_reg;
  626. map->cache_type = config->cache_type;
  627. spin_lock_init(&map->async_lock);
  628. INIT_LIST_HEAD(&map->async_list);
  629. INIT_LIST_HEAD(&map->async_free);
  630. init_waitqueue_head(&map->async_waitq);
  631. if (config->read_flag_mask ||
  632. config->write_flag_mask ||
  633. config->zero_flag_mask) {
  634. map->read_flag_mask = config->read_flag_mask;
  635. map->write_flag_mask = config->write_flag_mask;
  636. } else if (bus) {
  637. map->read_flag_mask = bus->read_flag_mask;
  638. }
  639. if (!bus) {
  640. map->reg_read = config->reg_read;
  641. map->reg_write = config->reg_write;
  642. map->defer_caching = false;
  643. goto skip_format_initialization;
  644. } else if (!bus->read || !bus->write) {
  645. map->reg_read = _regmap_bus_reg_read;
  646. map->reg_write = _regmap_bus_reg_write;
  647. map->defer_caching = false;
  648. goto skip_format_initialization;
  649. } else {
  650. map->reg_read = _regmap_bus_read;
  651. map->reg_update_bits = bus->reg_update_bits;
  652. }
  653. reg_endian = regmap_get_reg_endian(bus, config);
  654. val_endian = regmap_get_val_endian(dev, bus, config);
  655. switch (config->reg_bits + map->reg_shift) {
  656. case 2:
  657. switch (config->val_bits) {
  658. case 6:
  659. map->format.format_write = regmap_format_2_6_write;
  660. break;
  661. default:
  662. goto err_hwlock;
  663. }
  664. break;
  665. case 4:
  666. switch (config->val_bits) {
  667. case 12:
  668. map->format.format_write = regmap_format_4_12_write;
  669. break;
  670. default:
  671. goto err_hwlock;
  672. }
  673. break;
  674. case 7:
  675. switch (config->val_bits) {
  676. case 9:
  677. map->format.format_write = regmap_format_7_9_write;
  678. break;
  679. default:
  680. goto err_hwlock;
  681. }
  682. break;
  683. case 10:
  684. switch (config->val_bits) {
  685. case 14:
  686. map->format.format_write = regmap_format_10_14_write;
  687. break;
  688. default:
  689. goto err_hwlock;
  690. }
  691. break;
  692. case 8:
  693. map->format.format_reg = regmap_format_8;
  694. break;
  695. case 16:
  696. switch (reg_endian) {
  697. case REGMAP_ENDIAN_BIG:
  698. map->format.format_reg = regmap_format_16_be;
  699. break;
  700. case REGMAP_ENDIAN_LITTLE:
  701. map->format.format_reg = regmap_format_16_le;
  702. break;
  703. case REGMAP_ENDIAN_NATIVE:
  704. map->format.format_reg = regmap_format_16_native;
  705. break;
  706. default:
  707. goto err_hwlock;
  708. }
  709. break;
  710. case 24:
  711. if (reg_endian != REGMAP_ENDIAN_BIG)
  712. goto err_hwlock;
  713. map->format.format_reg = regmap_format_24;
  714. break;
  715. case 32:
  716. switch (reg_endian) {
  717. case REGMAP_ENDIAN_BIG:
  718. map->format.format_reg = regmap_format_32_be;
  719. break;
  720. case REGMAP_ENDIAN_LITTLE:
  721. map->format.format_reg = regmap_format_32_le;
  722. break;
  723. case REGMAP_ENDIAN_NATIVE:
  724. map->format.format_reg = regmap_format_32_native;
  725. break;
  726. default:
  727. goto err_hwlock;
  728. }
  729. break;
  730. #ifdef CONFIG_64BIT
  731. case 64:
  732. switch (reg_endian) {
  733. case REGMAP_ENDIAN_BIG:
  734. map->format.format_reg = regmap_format_64_be;
  735. break;
  736. case REGMAP_ENDIAN_LITTLE:
  737. map->format.format_reg = regmap_format_64_le;
  738. break;
  739. case REGMAP_ENDIAN_NATIVE:
  740. map->format.format_reg = regmap_format_64_native;
  741. break;
  742. default:
  743. goto err_hwlock;
  744. }
  745. break;
  746. #endif
  747. default:
  748. goto err_hwlock;
  749. }
  750. if (val_endian == REGMAP_ENDIAN_NATIVE)
  751. map->format.parse_inplace = regmap_parse_inplace_noop;
  752. switch (config->val_bits) {
  753. case 8:
  754. map->format.format_val = regmap_format_8;
  755. map->format.parse_val = regmap_parse_8;
  756. map->format.parse_inplace = regmap_parse_inplace_noop;
  757. break;
  758. case 16:
  759. switch (val_endian) {
  760. case REGMAP_ENDIAN_BIG:
  761. map->format.format_val = regmap_format_16_be;
  762. map->format.parse_val = regmap_parse_16_be;
  763. map->format.parse_inplace = regmap_parse_16_be_inplace;
  764. break;
  765. case REGMAP_ENDIAN_LITTLE:
  766. map->format.format_val = regmap_format_16_le;
  767. map->format.parse_val = regmap_parse_16_le;
  768. map->format.parse_inplace = regmap_parse_16_le_inplace;
  769. break;
  770. case REGMAP_ENDIAN_NATIVE:
  771. map->format.format_val = regmap_format_16_native;
  772. map->format.parse_val = regmap_parse_16_native;
  773. break;
  774. default:
  775. goto err_hwlock;
  776. }
  777. break;
  778. case 24:
  779. if (val_endian != REGMAP_ENDIAN_BIG)
  780. goto err_hwlock;
  781. map->format.format_val = regmap_format_24;
  782. map->format.parse_val = regmap_parse_24;
  783. break;
  784. case 32:
  785. switch (val_endian) {
  786. case REGMAP_ENDIAN_BIG:
  787. map->format.format_val = regmap_format_32_be;
  788. map->format.parse_val = regmap_parse_32_be;
  789. map->format.parse_inplace = regmap_parse_32_be_inplace;
  790. break;
  791. case REGMAP_ENDIAN_LITTLE:
  792. map->format.format_val = regmap_format_32_le;
  793. map->format.parse_val = regmap_parse_32_le;
  794. map->format.parse_inplace = regmap_parse_32_le_inplace;
  795. break;
  796. case REGMAP_ENDIAN_NATIVE:
  797. map->format.format_val = regmap_format_32_native;
  798. map->format.parse_val = regmap_parse_32_native;
  799. break;
  800. default:
  801. goto err_hwlock;
  802. }
  803. break;
  804. #ifdef CONFIG_64BIT
  805. case 64:
  806. switch (val_endian) {
  807. case REGMAP_ENDIAN_BIG:
  808. map->format.format_val = regmap_format_64_be;
  809. map->format.parse_val = regmap_parse_64_be;
  810. map->format.parse_inplace = regmap_parse_64_be_inplace;
  811. break;
  812. case REGMAP_ENDIAN_LITTLE:
  813. map->format.format_val = regmap_format_64_le;
  814. map->format.parse_val = regmap_parse_64_le;
  815. map->format.parse_inplace = regmap_parse_64_le_inplace;
  816. break;
  817. case REGMAP_ENDIAN_NATIVE:
  818. map->format.format_val = regmap_format_64_native;
  819. map->format.parse_val = regmap_parse_64_native;
  820. break;
  821. default:
  822. goto err_hwlock;
  823. }
  824. break;
  825. #endif
  826. }
  827. if (map->format.format_write) {
  828. if ((reg_endian != REGMAP_ENDIAN_BIG) ||
  829. (val_endian != REGMAP_ENDIAN_BIG))
  830. goto err_hwlock;
  831. map->use_single_write = true;
  832. }
  833. if (!map->format.format_write &&
  834. !(map->format.format_reg && map->format.format_val))
  835. goto err_hwlock;
  836. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  837. if (map->work_buf == NULL) {
  838. ret = -ENOMEM;
  839. goto err_hwlock;
  840. }
  841. if (map->format.format_write) {
  842. map->defer_caching = false;
  843. map->reg_write = _regmap_bus_formatted_write;
  844. } else if (map->format.format_val) {
  845. map->defer_caching = true;
  846. map->reg_write = _regmap_bus_raw_write;
  847. }
  848. skip_format_initialization:
  849. map->range_tree = RB_ROOT;
  850. for (i = 0; i < config->num_ranges; i++) {
  851. const struct regmap_range_cfg *range_cfg = &config->ranges[i];
  852. struct regmap_range_node *new;
  853. /* Sanity check */
  854. if (range_cfg->range_max < range_cfg->range_min) {
  855. dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
  856. range_cfg->range_max, range_cfg->range_min);
  857. goto err_range;
  858. }
  859. if (range_cfg->range_max > map->max_register) {
  860. dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
  861. range_cfg->range_max, map->max_register);
  862. goto err_range;
  863. }
  864. if (range_cfg->selector_reg > map->max_register) {
  865. dev_err(map->dev,
  866. "Invalid range %d: selector out of map\n", i);
  867. goto err_range;
  868. }
  869. if (range_cfg->window_len == 0) {
  870. dev_err(map->dev, "Invalid range %d: window_len 0\n",
  871. i);
  872. goto err_range;
  873. }
  874. /* Make sure, that this register range has no selector
  875. or data window within its boundary */
  876. for (j = 0; j < config->num_ranges; j++) {
  877. unsigned sel_reg = config->ranges[j].selector_reg;
  878. unsigned win_min = config->ranges[j].window_start;
  879. unsigned win_max = win_min +
  880. config->ranges[j].window_len - 1;
  881. /* Allow data window inside its own virtual range */
  882. if (j == i)
  883. continue;
  884. if (range_cfg->range_min <= sel_reg &&
  885. sel_reg <= range_cfg->range_max) {
  886. dev_err(map->dev,
  887. "Range %d: selector for %d in window\n",
  888. i, j);
  889. goto err_range;
  890. }
  891. if (!(win_max < range_cfg->range_min ||
  892. win_min > range_cfg->range_max)) {
  893. dev_err(map->dev,
  894. "Range %d: window for %d in window\n",
  895. i, j);
  896. goto err_range;
  897. }
  898. }
  899. new = kzalloc(sizeof(*new), GFP_KERNEL);
  900. if (new == NULL) {
  901. ret = -ENOMEM;
  902. goto err_range;
  903. }
  904. new->map = map;
  905. new->name = range_cfg->name;
  906. new->range_min = range_cfg->range_min;
  907. new->range_max = range_cfg->range_max;
  908. new->selector_reg = range_cfg->selector_reg;
  909. new->selector_mask = range_cfg->selector_mask;
  910. new->selector_shift = range_cfg->selector_shift;
  911. new->window_start = range_cfg->window_start;
  912. new->window_len = range_cfg->window_len;
  913. if (!_regmap_range_add(map, new)) {
  914. dev_err(map->dev, "Failed to add range %d\n", i);
  915. kfree(new);
  916. goto err_range;
  917. }
  918. if (map->selector_work_buf == NULL) {
  919. map->selector_work_buf =
  920. kzalloc(map->format.buf_size, GFP_KERNEL);
  921. if (map->selector_work_buf == NULL) {
  922. ret = -ENOMEM;
  923. goto err_range;
  924. }
  925. }
  926. }
  927. ret = regcache_init(map, config);
  928. if (ret != 0)
  929. goto err_range;
  930. if (dev) {
  931. ret = regmap_attach_dev(dev, map, config);
  932. if (ret != 0)
  933. goto err_regcache;
  934. } else {
  935. regmap_debugfs_init(map, config->name);
  936. }
  937. return map;
  938. err_regcache:
  939. regcache_exit(map);
  940. err_range:
  941. regmap_range_exit(map);
  942. kfree(map->work_buf);
  943. err_hwlock:
  944. if (map->hwlock)
  945. hwspin_lock_free(map->hwlock);
  946. err_name:
  947. kfree_const(map->name);
  948. err_map:
  949. kfree(map);
  950. err:
  951. return ERR_PTR(ret);
  952. }
  953. EXPORT_SYMBOL_GPL(__regmap_init);
  954. static void devm_regmap_release(struct device *dev, void *res)
  955. {
  956. regmap_exit(*(struct regmap **)res);
  957. }
  958. struct regmap *__devm_regmap_init(struct device *dev,
  959. const struct regmap_bus *bus,
  960. void *bus_context,
  961. const struct regmap_config *config,
  962. struct lock_class_key *lock_key,
  963. const char *lock_name)
  964. {
  965. struct regmap **ptr, *regmap;
  966. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  967. if (!ptr)
  968. return ERR_PTR(-ENOMEM);
  969. regmap = __regmap_init(dev, bus, bus_context, config,
  970. lock_key, lock_name);
  971. if (!IS_ERR(regmap)) {
  972. *ptr = regmap;
  973. devres_add(dev, ptr);
  974. } else {
  975. devres_free(ptr);
  976. }
  977. return regmap;
  978. }
  979. EXPORT_SYMBOL_GPL(__devm_regmap_init);
  980. static void regmap_field_init(struct regmap_field *rm_field,
  981. struct regmap *regmap, struct reg_field reg_field)
  982. {
  983. rm_field->regmap = regmap;
  984. rm_field->reg = reg_field.reg;
  985. rm_field->shift = reg_field.lsb;
  986. rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
  987. rm_field->id_size = reg_field.id_size;
  988. rm_field->id_offset = reg_field.id_offset;
  989. }
  990. /**
  991. * devm_regmap_field_alloc() - Allocate and initialise a register field.
  992. *
  993. * @dev: Device that will be interacted with
  994. * @regmap: regmap bank in which this register field is located.
  995. * @reg_field: Register field with in the bank.
  996. *
  997. * The return value will be an ERR_PTR() on error or a valid pointer
  998. * to a struct regmap_field. The regmap_field will be automatically freed
  999. * by the device management code.
  1000. */
  1001. struct regmap_field *devm_regmap_field_alloc(struct device *dev,
  1002. struct regmap *regmap, struct reg_field reg_field)
  1003. {
  1004. struct regmap_field *rm_field = devm_kzalloc(dev,
  1005. sizeof(*rm_field), GFP_KERNEL);
  1006. if (!rm_field)
  1007. return ERR_PTR(-ENOMEM);
  1008. regmap_field_init(rm_field, regmap, reg_field);
  1009. return rm_field;
  1010. }
  1011. EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
  1012. /**
  1013. * devm_regmap_field_free() - Free a register field allocated using
  1014. * devm_regmap_field_alloc.
  1015. *
  1016. * @dev: Device that will be interacted with
  1017. * @field: regmap field which should be freed.
  1018. *
  1019. * Free register field allocated using devm_regmap_field_alloc(). Usually
  1020. * drivers need not call this function, as the memory allocated via devm
  1021. * will be freed as per device-driver life-cyle.
  1022. */
  1023. void devm_regmap_field_free(struct device *dev,
  1024. struct regmap_field *field)
  1025. {
  1026. devm_kfree(dev, field);
  1027. }
  1028. EXPORT_SYMBOL_GPL(devm_regmap_field_free);
  1029. /**
  1030. * regmap_field_alloc() - Allocate and initialise a register field.
  1031. *
  1032. * @regmap: regmap bank in which this register field is located.
  1033. * @reg_field: Register field with in the bank.
  1034. *
  1035. * The return value will be an ERR_PTR() on error or a valid pointer
  1036. * to a struct regmap_field. The regmap_field should be freed by the
  1037. * user once its finished working with it using regmap_field_free().
  1038. */
  1039. struct regmap_field *regmap_field_alloc(struct regmap *regmap,
  1040. struct reg_field reg_field)
  1041. {
  1042. struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
  1043. if (!rm_field)
  1044. return ERR_PTR(-ENOMEM);
  1045. regmap_field_init(rm_field, regmap, reg_field);
  1046. return rm_field;
  1047. }
  1048. EXPORT_SYMBOL_GPL(regmap_field_alloc);
  1049. /**
  1050. * regmap_field_free() - Free register field allocated using
  1051. * regmap_field_alloc.
  1052. *
  1053. * @field: regmap field which should be freed.
  1054. */
  1055. void regmap_field_free(struct regmap_field *field)
  1056. {
  1057. kfree(field);
  1058. }
  1059. EXPORT_SYMBOL_GPL(regmap_field_free);
  1060. /**
  1061. * regmap_reinit_cache() - Reinitialise the current register cache
  1062. *
  1063. * @map: Register map to operate on.
  1064. * @config: New configuration. Only the cache data will be used.
  1065. *
  1066. * Discard any existing register cache for the map and initialize a
  1067. * new cache. This can be used to restore the cache to defaults or to
  1068. * update the cache configuration to reflect runtime discovery of the
  1069. * hardware.
  1070. *
  1071. * No explicit locking is done here, the user needs to ensure that
  1072. * this function will not race with other calls to regmap.
  1073. */
  1074. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  1075. {
  1076. regcache_exit(map);
  1077. regmap_debugfs_exit(map);
  1078. map->max_register = config->max_register;
  1079. map->writeable_reg = config->writeable_reg;
  1080. map->readable_reg = config->readable_reg;
  1081. map->volatile_reg = config->volatile_reg;
  1082. map->precious_reg = config->precious_reg;
  1083. map->readable_noinc_reg = config->readable_noinc_reg;
  1084. map->cache_type = config->cache_type;
  1085. regmap_debugfs_init(map, config->name);
  1086. map->cache_bypass = false;
  1087. map->cache_only = false;
  1088. return regcache_init(map, config);
  1089. }
  1090. EXPORT_SYMBOL_GPL(regmap_reinit_cache);
  1091. /**
  1092. * regmap_exit() - Free a previously allocated register map
  1093. *
  1094. * @map: Register map to operate on.
  1095. */
  1096. void regmap_exit(struct regmap *map)
  1097. {
  1098. struct regmap_async *async;
  1099. regcache_exit(map);
  1100. regmap_debugfs_exit(map);
  1101. regmap_range_exit(map);
  1102. if (map->bus && map->bus->free_context)
  1103. map->bus->free_context(map->bus_context);
  1104. kfree(map->work_buf);
  1105. while (!list_empty(&map->async_free)) {
  1106. async = list_first_entry_or_null(&map->async_free,
  1107. struct regmap_async,
  1108. list);
  1109. list_del(&async->list);
  1110. kfree(async->work_buf);
  1111. kfree(async);
  1112. }
  1113. if (map->hwlock)
  1114. hwspin_lock_free(map->hwlock);
  1115. kfree_const(map->name);
  1116. kfree(map);
  1117. }
  1118. EXPORT_SYMBOL_GPL(regmap_exit);
  1119. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  1120. {
  1121. struct regmap **r = res;
  1122. if (!r || !*r) {
  1123. WARN_ON(!r || !*r);
  1124. return 0;
  1125. }
  1126. /* If the user didn't specify a name match any */
  1127. if (data)
  1128. return (*r)->name == data;
  1129. else
  1130. return 1;
  1131. }
  1132. /**
  1133. * dev_get_regmap() - Obtain the regmap (if any) for a device
  1134. *
  1135. * @dev: Device to retrieve the map for
  1136. * @name: Optional name for the register map, usually NULL.
  1137. *
  1138. * Returns the regmap for the device if one is present, or NULL. If
  1139. * name is specified then it must match the name specified when
  1140. * registering the device, if it is NULL then the first regmap found
  1141. * will be used. Devices with multiple register maps are very rare,
  1142. * generic code should normally not need to specify a name.
  1143. */
  1144. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  1145. {
  1146. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  1147. dev_get_regmap_match, (void *)name);
  1148. if (!r)
  1149. return NULL;
  1150. return *r;
  1151. }
  1152. EXPORT_SYMBOL_GPL(dev_get_regmap);
  1153. /**
  1154. * regmap_get_device() - Obtain the device from a regmap
  1155. *
  1156. * @map: Register map to operate on.
  1157. *
  1158. * Returns the underlying device that the regmap has been created for.
  1159. */
  1160. struct device *regmap_get_device(struct regmap *map)
  1161. {
  1162. return map->dev;
  1163. }
  1164. EXPORT_SYMBOL_GPL(regmap_get_device);
  1165. static int _regmap_select_page(struct regmap *map, unsigned int *reg,
  1166. struct regmap_range_node *range,
  1167. unsigned int val_num)
  1168. {
  1169. void *orig_work_buf;
  1170. unsigned int win_offset;
  1171. unsigned int win_page;
  1172. bool page_chg;
  1173. int ret;
  1174. win_offset = (*reg - range->range_min) % range->window_len;
  1175. win_page = (*reg - range->range_min) / range->window_len;
  1176. if (val_num > 1) {
  1177. /* Bulk write shouldn't cross range boundary */
  1178. if (*reg + val_num - 1 > range->range_max)
  1179. return -EINVAL;
  1180. /* ... or single page boundary */
  1181. if (val_num > range->window_len - win_offset)
  1182. return -EINVAL;
  1183. }
  1184. /* It is possible to have selector register inside data window.
  1185. In that case, selector register is located on every page and
  1186. it needs no page switching, when accessed alone. */
  1187. if (val_num > 1 ||
  1188. range->window_start + win_offset != range->selector_reg) {
  1189. /* Use separate work_buf during page switching */
  1190. orig_work_buf = map->work_buf;
  1191. map->work_buf = map->selector_work_buf;
  1192. ret = _regmap_update_bits(map, range->selector_reg,
  1193. range->selector_mask,
  1194. win_page << range->selector_shift,
  1195. &page_chg, false);
  1196. map->work_buf = orig_work_buf;
  1197. if (ret != 0)
  1198. return ret;
  1199. }
  1200. *reg = range->window_start + win_offset;
  1201. return 0;
  1202. }
  1203. static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
  1204. unsigned long mask)
  1205. {
  1206. u8 *buf;
  1207. int i;
  1208. if (!mask || !map->work_buf)
  1209. return;
  1210. buf = map->work_buf;
  1211. for (i = 0; i < max_bytes; i++)
  1212. buf[i] |= (mask >> (8 * i)) & 0xff;
  1213. }
  1214. static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
  1215. const void *val, size_t val_len)
  1216. {
  1217. struct regmap_range_node *range;
  1218. unsigned long flags;
  1219. void *work_val = map->work_buf + map->format.reg_bytes +
  1220. map->format.pad_bytes;
  1221. void *buf;
  1222. int ret = -ENOTSUPP;
  1223. size_t len;
  1224. int i;
  1225. WARN_ON(!map->bus);
  1226. /* Check for unwritable registers before we start */
  1227. if (map->writeable_reg)
  1228. for (i = 0; i < val_len / map->format.val_bytes; i++)
  1229. if (!map->writeable_reg(map->dev,
  1230. reg + regmap_get_offset(map, i)))
  1231. return -EINVAL;
  1232. if (!map->cache_bypass && map->format.parse_val) {
  1233. unsigned int ival;
  1234. int val_bytes = map->format.val_bytes;
  1235. for (i = 0; i < val_len / val_bytes; i++) {
  1236. ival = map->format.parse_val(val + (i * val_bytes));
  1237. ret = regcache_write(map,
  1238. reg + regmap_get_offset(map, i),
  1239. ival);
  1240. if (ret) {
  1241. dev_err(map->dev,
  1242. "Error in caching of register: %x ret: %d\n",
  1243. reg + i, ret);
  1244. return ret;
  1245. }
  1246. }
  1247. if (map->cache_only) {
  1248. map->cache_dirty = true;
  1249. return 0;
  1250. }
  1251. }
  1252. range = _regmap_range_lookup(map, reg);
  1253. if (range) {
  1254. int val_num = val_len / map->format.val_bytes;
  1255. int win_offset = (reg - range->range_min) % range->window_len;
  1256. int win_residue = range->window_len - win_offset;
  1257. /* If the write goes beyond the end of the window split it */
  1258. while (val_num > win_residue) {
  1259. dev_dbg(map->dev, "Writing window %d/%zu\n",
  1260. win_residue, val_len / map->format.val_bytes);
  1261. ret = _regmap_raw_write_impl(map, reg, val,
  1262. win_residue *
  1263. map->format.val_bytes);
  1264. if (ret != 0)
  1265. return ret;
  1266. reg += win_residue;
  1267. val_num -= win_residue;
  1268. val += win_residue * map->format.val_bytes;
  1269. val_len -= win_residue * map->format.val_bytes;
  1270. win_offset = (reg - range->range_min) %
  1271. range->window_len;
  1272. win_residue = range->window_len - win_offset;
  1273. }
  1274. ret = _regmap_select_page(map, &reg, range, val_num);
  1275. if (ret != 0)
  1276. return ret;
  1277. }
  1278. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1279. regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
  1280. map->write_flag_mask);
  1281. /*
  1282. * Essentially all I/O mechanisms will be faster with a single
  1283. * buffer to write. Since register syncs often generate raw
  1284. * writes of single registers optimise that case.
  1285. */
  1286. if (val != work_val && val_len == map->format.val_bytes) {
  1287. memcpy(work_val, val, map->format.val_bytes);
  1288. val = work_val;
  1289. }
  1290. if (map->async && map->bus->async_write) {
  1291. struct regmap_async *async;
  1292. trace_regmap_async_write_start(map, reg, val_len);
  1293. spin_lock_irqsave(&map->async_lock, flags);
  1294. async = list_first_entry_or_null(&map->async_free,
  1295. struct regmap_async,
  1296. list);
  1297. if (async)
  1298. list_del(&async->list);
  1299. spin_unlock_irqrestore(&map->async_lock, flags);
  1300. if (!async) {
  1301. async = map->bus->async_alloc();
  1302. if (!async)
  1303. return -ENOMEM;
  1304. async->work_buf = kzalloc(map->format.buf_size,
  1305. GFP_KERNEL | GFP_DMA);
  1306. if (!async->work_buf) {
  1307. kfree(async);
  1308. return -ENOMEM;
  1309. }
  1310. }
  1311. async->map = map;
  1312. /* If the caller supplied the value we can use it safely. */
  1313. memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
  1314. map->format.reg_bytes + map->format.val_bytes);
  1315. spin_lock_irqsave(&map->async_lock, flags);
  1316. list_add_tail(&async->list, &map->async_list);
  1317. spin_unlock_irqrestore(&map->async_lock, flags);
  1318. if (val != work_val)
  1319. ret = map->bus->async_write(map->bus_context,
  1320. async->work_buf,
  1321. map->format.reg_bytes +
  1322. map->format.pad_bytes,
  1323. val, val_len, async);
  1324. else
  1325. ret = map->bus->async_write(map->bus_context,
  1326. async->work_buf,
  1327. map->format.reg_bytes +
  1328. map->format.pad_bytes +
  1329. val_len, NULL, 0, async);
  1330. if (ret != 0) {
  1331. dev_err(map->dev, "Failed to schedule write: %d\n",
  1332. ret);
  1333. spin_lock_irqsave(&map->async_lock, flags);
  1334. list_move(&async->list, &map->async_free);
  1335. spin_unlock_irqrestore(&map->async_lock, flags);
  1336. }
  1337. return ret;
  1338. }
  1339. trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
  1340. /* If we're doing a single register write we can probably just
  1341. * send the work_buf directly, otherwise try to do a gather
  1342. * write.
  1343. */
  1344. if (val == work_val)
  1345. ret = map->bus->write(map->bus_context, map->work_buf,
  1346. map->format.reg_bytes +
  1347. map->format.pad_bytes +
  1348. val_len);
  1349. else if (map->bus->gather_write)
  1350. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  1351. map->format.reg_bytes +
  1352. map->format.pad_bytes,
  1353. val, val_len);
  1354. else
  1355. ret = -ENOTSUPP;
  1356. /* If that didn't work fall back on linearising by hand. */
  1357. if (ret == -ENOTSUPP) {
  1358. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  1359. buf = kzalloc(len, GFP_KERNEL);
  1360. if (!buf)
  1361. return -ENOMEM;
  1362. memcpy(buf, map->work_buf, map->format.reg_bytes);
  1363. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  1364. val, val_len);
  1365. ret = map->bus->write(map->bus_context, buf, len);
  1366. kfree(buf);
  1367. } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
  1368. /* regcache_drop_region() takes lock that we already have,
  1369. * thus call map->cache_ops->drop() directly
  1370. */
  1371. if (map->cache_ops && map->cache_ops->drop)
  1372. map->cache_ops->drop(map, reg, reg + 1);
  1373. }
  1374. trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
  1375. return ret;
  1376. }
  1377. /**
  1378. * regmap_can_raw_write - Test if regmap_raw_write() is supported
  1379. *
  1380. * @map: Map to check.
  1381. */
  1382. bool regmap_can_raw_write(struct regmap *map)
  1383. {
  1384. return map->bus && map->bus->write && map->format.format_val &&
  1385. map->format.format_reg;
  1386. }
  1387. EXPORT_SYMBOL_GPL(regmap_can_raw_write);
  1388. /**
  1389. * regmap_get_raw_read_max - Get the maximum size we can read
  1390. *
  1391. * @map: Map to check.
  1392. */
  1393. size_t regmap_get_raw_read_max(struct regmap *map)
  1394. {
  1395. return map->max_raw_read;
  1396. }
  1397. EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
  1398. /**
  1399. * regmap_get_raw_write_max - Get the maximum size we can read
  1400. *
  1401. * @map: Map to check.
  1402. */
  1403. size_t regmap_get_raw_write_max(struct regmap *map)
  1404. {
  1405. return map->max_raw_write;
  1406. }
  1407. EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
  1408. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  1409. unsigned int val)
  1410. {
  1411. int ret;
  1412. struct regmap_range_node *range;
  1413. struct regmap *map = context;
  1414. WARN_ON(!map->bus || !map->format.format_write);
  1415. range = _regmap_range_lookup(map, reg);
  1416. if (range) {
  1417. ret = _regmap_select_page(map, &reg, range, 1);
  1418. if (ret != 0)
  1419. return ret;
  1420. }
  1421. map->format.format_write(map, reg, val);
  1422. trace_regmap_hw_write_start(map, reg, 1);
  1423. ret = map->bus->write(map->bus_context, map->work_buf,
  1424. map->format.buf_size);
  1425. trace_regmap_hw_write_done(map, reg, 1);
  1426. return ret;
  1427. }
  1428. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  1429. unsigned int val)
  1430. {
  1431. struct regmap *map = context;
  1432. return map->bus->reg_write(map->bus_context, reg, val);
  1433. }
  1434. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  1435. unsigned int val)
  1436. {
  1437. struct regmap *map = context;
  1438. WARN_ON(!map->bus || !map->format.format_val);
  1439. map->format.format_val(map->work_buf + map->format.reg_bytes
  1440. + map->format.pad_bytes, val, 0);
  1441. return _regmap_raw_write_impl(map, reg,
  1442. map->work_buf +
  1443. map->format.reg_bytes +
  1444. map->format.pad_bytes,
  1445. map->format.val_bytes);
  1446. }
  1447. static inline void *_regmap_map_get_context(struct regmap *map)
  1448. {
  1449. return (map->bus) ? map : map->bus_context;
  1450. }
  1451. int _regmap_write(struct regmap *map, unsigned int reg,
  1452. unsigned int val)
  1453. {
  1454. int ret;
  1455. void *context = _regmap_map_get_context(map);
  1456. if (!regmap_writeable(map, reg))
  1457. return -EIO;
  1458. if (!map->cache_bypass && !map->defer_caching) {
  1459. ret = regcache_write(map, reg, val);
  1460. if (ret != 0)
  1461. return ret;
  1462. if (map->cache_only) {
  1463. map->cache_dirty = true;
  1464. return 0;
  1465. }
  1466. }
  1467. #ifdef LOG_DEVICE
  1468. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1469. dev_info(map->dev, "%x <= %x\n", reg, val);
  1470. #endif
  1471. trace_regmap_reg_write(map, reg, val);
  1472. return map->reg_write(context, reg, val);
  1473. }
  1474. /**
  1475. * regmap_write() - Write a value to a single register
  1476. *
  1477. * @map: Register map to write to
  1478. * @reg: Register to write to
  1479. * @val: Value to be written
  1480. *
  1481. * A value of zero will be returned on success, a negative errno will
  1482. * be returned in error cases.
  1483. */
  1484. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  1485. {
  1486. int ret;
  1487. if (!IS_ALIGNED(reg, map->reg_stride))
  1488. return -EINVAL;
  1489. map->lock(map->lock_arg);
  1490. ret = _regmap_write(map, reg, val);
  1491. map->unlock(map->lock_arg);
  1492. return ret;
  1493. }
  1494. EXPORT_SYMBOL_GPL(regmap_write);
  1495. /**
  1496. * regmap_write_async() - Write a value to a single register asynchronously
  1497. *
  1498. * @map: Register map to write to
  1499. * @reg: Register to write to
  1500. * @val: Value to be written
  1501. *
  1502. * A value of zero will be returned on success, a negative errno will
  1503. * be returned in error cases.
  1504. */
  1505. int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
  1506. {
  1507. int ret;
  1508. if (!IS_ALIGNED(reg, map->reg_stride))
  1509. return -EINVAL;
  1510. map->lock(map->lock_arg);
  1511. map->async = true;
  1512. ret = _regmap_write(map, reg, val);
  1513. map->async = false;
  1514. map->unlock(map->lock_arg);
  1515. return ret;
  1516. }
  1517. EXPORT_SYMBOL_GPL(regmap_write_async);
  1518. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  1519. const void *val, size_t val_len)
  1520. {
  1521. size_t val_bytes = map->format.val_bytes;
  1522. size_t val_count = val_len / val_bytes;
  1523. size_t chunk_count, chunk_bytes;
  1524. size_t chunk_regs = val_count;
  1525. int ret, i;
  1526. if (!val_count)
  1527. return -EINVAL;
  1528. if (map->use_single_write)
  1529. chunk_regs = 1;
  1530. else if (map->max_raw_write && val_len > map->max_raw_write)
  1531. chunk_regs = map->max_raw_write / val_bytes;
  1532. chunk_count = val_count / chunk_regs;
  1533. chunk_bytes = chunk_regs * val_bytes;
  1534. /* Write as many bytes as possible with chunk_size */
  1535. for (i = 0; i < chunk_count; i++) {
  1536. ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes);
  1537. if (ret)
  1538. return ret;
  1539. reg += regmap_get_offset(map, chunk_regs);
  1540. val += chunk_bytes;
  1541. val_len -= chunk_bytes;
  1542. }
  1543. /* Write remaining bytes */
  1544. if (val_len)
  1545. ret = _regmap_raw_write_impl(map, reg, val, val_len);
  1546. return ret;
  1547. }
  1548. /**
  1549. * regmap_raw_write() - Write raw values to one or more registers
  1550. *
  1551. * @map: Register map to write to
  1552. * @reg: Initial register to write to
  1553. * @val: Block of data to be written, laid out for direct transmission to the
  1554. * device
  1555. * @val_len: Length of data pointed to by val.
  1556. *
  1557. * This function is intended to be used for things like firmware
  1558. * download where a large block of data needs to be transferred to the
  1559. * device. No formatting will be done on the data provided.
  1560. *
  1561. * A value of zero will be returned on success, a negative errno will
  1562. * be returned in error cases.
  1563. */
  1564. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1565. const void *val, size_t val_len)
  1566. {
  1567. int ret;
  1568. if (!regmap_can_raw_write(map))
  1569. return -EINVAL;
  1570. if (val_len % map->format.val_bytes)
  1571. return -EINVAL;
  1572. map->lock(map->lock_arg);
  1573. ret = _regmap_raw_write(map, reg, val, val_len);
  1574. map->unlock(map->lock_arg);
  1575. return ret;
  1576. }
  1577. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1578. /**
  1579. * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
  1580. * register field.
  1581. *
  1582. * @field: Register field to write to
  1583. * @mask: Bitmask to change
  1584. * @val: Value to be written
  1585. * @change: Boolean indicating if a write was done
  1586. * @async: Boolean indicating asynchronously
  1587. * @force: Boolean indicating use force update
  1588. *
  1589. * Perform a read/modify/write cycle on the register field with change,
  1590. * async, force option.
  1591. *
  1592. * A value of zero will be returned on success, a negative errno will
  1593. * be returned in error cases.
  1594. */
  1595. int regmap_field_update_bits_base(struct regmap_field *field,
  1596. unsigned int mask, unsigned int val,
  1597. bool *change, bool async, bool force)
  1598. {
  1599. mask = (mask << field->shift) & field->mask;
  1600. return regmap_update_bits_base(field->regmap, field->reg,
  1601. mask, val << field->shift,
  1602. change, async, force);
  1603. }
  1604. EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
  1605. /**
  1606. * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
  1607. * register field with port ID
  1608. *
  1609. * @field: Register field to write to
  1610. * @id: port ID
  1611. * @mask: Bitmask to change
  1612. * @val: Value to be written
  1613. * @change: Boolean indicating if a write was done
  1614. * @async: Boolean indicating asynchronously
  1615. * @force: Boolean indicating use force update
  1616. *
  1617. * A value of zero will be returned on success, a negative errno will
  1618. * be returned in error cases.
  1619. */
  1620. int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
  1621. unsigned int mask, unsigned int val,
  1622. bool *change, bool async, bool force)
  1623. {
  1624. if (id >= field->id_size)
  1625. return -EINVAL;
  1626. mask = (mask << field->shift) & field->mask;
  1627. return regmap_update_bits_base(field->regmap,
  1628. field->reg + (field->id_offset * id),
  1629. mask, val << field->shift,
  1630. change, async, force);
  1631. }
  1632. EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
  1633. /**
  1634. * regmap_bulk_write() - Write multiple registers to the device
  1635. *
  1636. * @map: Register map to write to
  1637. * @reg: First register to be write from
  1638. * @val: Block of data to be written, in native register size for device
  1639. * @val_count: Number of registers to write
  1640. *
  1641. * This function is intended to be used for writing a large block of
  1642. * data to the device either in single transfer or multiple transfer.
  1643. *
  1644. * A value of zero will be returned on success, a negative errno will
  1645. * be returned in error cases.
  1646. */
  1647. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1648. size_t val_count)
  1649. {
  1650. int ret = 0, i;
  1651. size_t val_bytes = map->format.val_bytes;
  1652. if (!IS_ALIGNED(reg, map->reg_stride))
  1653. return -EINVAL;
  1654. /*
  1655. * Some devices don't support bulk write, for them we have a series of
  1656. * single write operations.
  1657. */
  1658. if (!map->bus || !map->format.parse_inplace) {
  1659. map->lock(map->lock_arg);
  1660. for (i = 0; i < val_count; i++) {
  1661. unsigned int ival;
  1662. switch (val_bytes) {
  1663. case 1:
  1664. ival = *(u8 *)(val + (i * val_bytes));
  1665. break;
  1666. case 2:
  1667. ival = *(u16 *)(val + (i * val_bytes));
  1668. break;
  1669. case 4:
  1670. ival = *(u32 *)(val + (i * val_bytes));
  1671. break;
  1672. #ifdef CONFIG_64BIT
  1673. case 8:
  1674. ival = *(u64 *)(val + (i * val_bytes));
  1675. break;
  1676. #endif
  1677. default:
  1678. ret = -EINVAL;
  1679. goto out;
  1680. }
  1681. ret = _regmap_write(map,
  1682. reg + regmap_get_offset(map, i),
  1683. ival);
  1684. if (ret != 0)
  1685. goto out;
  1686. }
  1687. out:
  1688. map->unlock(map->lock_arg);
  1689. } else {
  1690. void *wval;
  1691. wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
  1692. if (!wval)
  1693. return -ENOMEM;
  1694. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1695. map->format.parse_inplace(wval + i);
  1696. ret = regmap_raw_write(map, reg, wval, val_bytes * val_count);
  1697. kfree(wval);
  1698. }
  1699. return ret;
  1700. }
  1701. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1702. /*
  1703. * _regmap_raw_multi_reg_write()
  1704. *
  1705. * the (register,newvalue) pairs in regs have not been formatted, but
  1706. * they are all in the same page and have been changed to being page
  1707. * relative. The page register has been written if that was necessary.
  1708. */
  1709. static int _regmap_raw_multi_reg_write(struct regmap *map,
  1710. const struct reg_sequence *regs,
  1711. size_t num_regs)
  1712. {
  1713. int ret;
  1714. void *buf;
  1715. int i;
  1716. u8 *u8;
  1717. size_t val_bytes = map->format.val_bytes;
  1718. size_t reg_bytes = map->format.reg_bytes;
  1719. size_t pad_bytes = map->format.pad_bytes;
  1720. size_t pair_size = reg_bytes + pad_bytes + val_bytes;
  1721. size_t len = pair_size * num_regs;
  1722. if (!len)
  1723. return -EINVAL;
  1724. buf = kzalloc(len, GFP_KERNEL);
  1725. if (!buf)
  1726. return -ENOMEM;
  1727. /* We have to linearise by hand. */
  1728. u8 = buf;
  1729. for (i = 0; i < num_regs; i++) {
  1730. unsigned int reg = regs[i].reg;
  1731. unsigned int val = regs[i].def;
  1732. trace_regmap_hw_write_start(map, reg, 1);
  1733. map->format.format_reg(u8, reg, map->reg_shift);
  1734. u8 += reg_bytes + pad_bytes;
  1735. map->format.format_val(u8, val, 0);
  1736. u8 += val_bytes;
  1737. }
  1738. u8 = buf;
  1739. *u8 |= map->write_flag_mask;
  1740. ret = map->bus->write(map->bus_context, buf, len);
  1741. kfree(buf);
  1742. for (i = 0; i < num_regs; i++) {
  1743. int reg = regs[i].reg;
  1744. trace_regmap_hw_write_done(map, reg, 1);
  1745. }
  1746. return ret;
  1747. }
  1748. static unsigned int _regmap_register_page(struct regmap *map,
  1749. unsigned int reg,
  1750. struct regmap_range_node *range)
  1751. {
  1752. unsigned int win_page = (reg - range->range_min) / range->window_len;
  1753. return win_page;
  1754. }
  1755. static int _regmap_range_multi_paged_reg_write(struct regmap *map,
  1756. struct reg_sequence *regs,
  1757. size_t num_regs)
  1758. {
  1759. int ret;
  1760. int i, n;
  1761. struct reg_sequence *base;
  1762. unsigned int this_page = 0;
  1763. unsigned int page_change = 0;
  1764. /*
  1765. * the set of registers are not neccessarily in order, but
  1766. * since the order of write must be preserved this algorithm
  1767. * chops the set each time the page changes. This also applies
  1768. * if there is a delay required at any point in the sequence.
  1769. */
  1770. base = regs;
  1771. for (i = 0, n = 0; i < num_regs; i++, n++) {
  1772. unsigned int reg = regs[i].reg;
  1773. struct regmap_range_node *range;
  1774. range = _regmap_range_lookup(map, reg);
  1775. if (range) {
  1776. unsigned int win_page = _regmap_register_page(map, reg,
  1777. range);
  1778. if (i == 0)
  1779. this_page = win_page;
  1780. if (win_page != this_page) {
  1781. this_page = win_page;
  1782. page_change = 1;
  1783. }
  1784. }
  1785. /* If we have both a page change and a delay make sure to
  1786. * write the regs and apply the delay before we change the
  1787. * page.
  1788. */
  1789. if (page_change || regs[i].delay_us) {
  1790. /* For situations where the first write requires
  1791. * a delay we need to make sure we don't call
  1792. * raw_multi_reg_write with n=0
  1793. * This can't occur with page breaks as we
  1794. * never write on the first iteration
  1795. */
  1796. if (regs[i].delay_us && i == 0)
  1797. n = 1;
  1798. ret = _regmap_raw_multi_reg_write(map, base, n);
  1799. if (ret != 0)
  1800. return ret;
  1801. if (regs[i].delay_us)
  1802. udelay(regs[i].delay_us);
  1803. base += n;
  1804. n = 0;
  1805. if (page_change) {
  1806. ret = _regmap_select_page(map,
  1807. &base[n].reg,
  1808. range, 1);
  1809. if (ret != 0)
  1810. return ret;
  1811. page_change = 0;
  1812. }
  1813. }
  1814. }
  1815. if (n > 0)
  1816. return _regmap_raw_multi_reg_write(map, base, n);
  1817. return 0;
  1818. }
  1819. static int _regmap_multi_reg_write(struct regmap *map,
  1820. const struct reg_sequence *regs,
  1821. size_t num_regs)
  1822. {
  1823. int i;
  1824. int ret;
  1825. if (!map->can_multi_write) {
  1826. for (i = 0; i < num_regs; i++) {
  1827. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1828. if (ret != 0)
  1829. return ret;
  1830. if (regs[i].delay_us)
  1831. udelay(regs[i].delay_us);
  1832. }
  1833. return 0;
  1834. }
  1835. if (!map->format.parse_inplace)
  1836. return -EINVAL;
  1837. if (map->writeable_reg)
  1838. for (i = 0; i < num_regs; i++) {
  1839. int reg = regs[i].reg;
  1840. if (!map->writeable_reg(map->dev, reg))
  1841. return -EINVAL;
  1842. if (!IS_ALIGNED(reg, map->reg_stride))
  1843. return -EINVAL;
  1844. }
  1845. if (!map->cache_bypass) {
  1846. for (i = 0; i < num_regs; i++) {
  1847. unsigned int val = regs[i].def;
  1848. unsigned int reg = regs[i].reg;
  1849. ret = regcache_write(map, reg, val);
  1850. if (ret) {
  1851. dev_err(map->dev,
  1852. "Error in caching of register: %x ret: %d\n",
  1853. reg, ret);
  1854. return ret;
  1855. }
  1856. }
  1857. if (map->cache_only) {
  1858. map->cache_dirty = true;
  1859. return 0;
  1860. }
  1861. }
  1862. WARN_ON(!map->bus);
  1863. for (i = 0; i < num_regs; i++) {
  1864. unsigned int reg = regs[i].reg;
  1865. struct regmap_range_node *range;
  1866. /* Coalesce all the writes between a page break or a delay
  1867. * in a sequence
  1868. */
  1869. range = _regmap_range_lookup(map, reg);
  1870. if (range || regs[i].delay_us) {
  1871. size_t len = sizeof(struct reg_sequence)*num_regs;
  1872. struct reg_sequence *base = kmemdup(regs, len,
  1873. GFP_KERNEL);
  1874. if (!base)
  1875. return -ENOMEM;
  1876. ret = _regmap_range_multi_paged_reg_write(map, base,
  1877. num_regs);
  1878. kfree(base);
  1879. return ret;
  1880. }
  1881. }
  1882. return _regmap_raw_multi_reg_write(map, regs, num_regs);
  1883. }
  1884. /**
  1885. * regmap_multi_reg_write() - Write multiple registers to the device
  1886. *
  1887. * @map: Register map to write to
  1888. * @regs: Array of structures containing register,value to be written
  1889. * @num_regs: Number of registers to write
  1890. *
  1891. * Write multiple registers to the device where the set of register, value
  1892. * pairs are supplied in any order, possibly not all in a single range.
  1893. *
  1894. * The 'normal' block write mode will send ultimately send data on the
  1895. * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
  1896. * addressed. However, this alternative block multi write mode will send
  1897. * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
  1898. * must of course support the mode.
  1899. *
  1900. * A value of zero will be returned on success, a negative errno will be
  1901. * returned in error cases.
  1902. */
  1903. int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
  1904. int num_regs)
  1905. {
  1906. int ret;
  1907. map->lock(map->lock_arg);
  1908. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1909. map->unlock(map->lock_arg);
  1910. return ret;
  1911. }
  1912. EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
  1913. /**
  1914. * regmap_multi_reg_write_bypassed() - Write multiple registers to the
  1915. * device but not the cache
  1916. *
  1917. * @map: Register map to write to
  1918. * @regs: Array of structures containing register,value to be written
  1919. * @num_regs: Number of registers to write
  1920. *
  1921. * Write multiple registers to the device but not the cache where the set
  1922. * of register are supplied in any order.
  1923. *
  1924. * This function is intended to be used for writing a large block of data
  1925. * atomically to the device in single transfer for those I2C client devices
  1926. * that implement this alternative block write mode.
  1927. *
  1928. * A value of zero will be returned on success, a negative errno will
  1929. * be returned in error cases.
  1930. */
  1931. int regmap_multi_reg_write_bypassed(struct regmap *map,
  1932. const struct reg_sequence *regs,
  1933. int num_regs)
  1934. {
  1935. int ret;
  1936. bool bypass;
  1937. map->lock(map->lock_arg);
  1938. bypass = map->cache_bypass;
  1939. map->cache_bypass = true;
  1940. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1941. map->cache_bypass = bypass;
  1942. map->unlock(map->lock_arg);
  1943. return ret;
  1944. }
  1945. EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
  1946. /**
  1947. * regmap_raw_write_async() - Write raw values to one or more registers
  1948. * asynchronously
  1949. *
  1950. * @map: Register map to write to
  1951. * @reg: Initial register to write to
  1952. * @val: Block of data to be written, laid out for direct transmission to the
  1953. * device. Must be valid until regmap_async_complete() is called.
  1954. * @val_len: Length of data pointed to by val.
  1955. *
  1956. * This function is intended to be used for things like firmware
  1957. * download where a large block of data needs to be transferred to the
  1958. * device. No formatting will be done on the data provided.
  1959. *
  1960. * If supported by the underlying bus the write will be scheduled
  1961. * asynchronously, helping maximise I/O speed on higher speed buses
  1962. * like SPI. regmap_async_complete() can be called to ensure that all
  1963. * asynchrnous writes have been completed.
  1964. *
  1965. * A value of zero will be returned on success, a negative errno will
  1966. * be returned in error cases.
  1967. */
  1968. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  1969. const void *val, size_t val_len)
  1970. {
  1971. int ret;
  1972. if (val_len % map->format.val_bytes)
  1973. return -EINVAL;
  1974. if (!IS_ALIGNED(reg, map->reg_stride))
  1975. return -EINVAL;
  1976. map->lock(map->lock_arg);
  1977. map->async = true;
  1978. ret = _regmap_raw_write(map, reg, val, val_len);
  1979. map->async = false;
  1980. map->unlock(map->lock_arg);
  1981. return ret;
  1982. }
  1983. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  1984. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1985. unsigned int val_len)
  1986. {
  1987. struct regmap_range_node *range;
  1988. int ret;
  1989. WARN_ON(!map->bus);
  1990. if (!map->bus || !map->bus->read)
  1991. return -EINVAL;
  1992. range = _regmap_range_lookup(map, reg);
  1993. if (range) {
  1994. ret = _regmap_select_page(map, &reg, range,
  1995. val_len / map->format.val_bytes);
  1996. if (ret != 0)
  1997. return ret;
  1998. }
  1999. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  2000. regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
  2001. map->read_flag_mask);
  2002. trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
  2003. ret = map->bus->read(map->bus_context, map->work_buf,
  2004. map->format.reg_bytes + map->format.pad_bytes,
  2005. val, val_len);
  2006. trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
  2007. return ret;
  2008. }
  2009. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  2010. unsigned int *val)
  2011. {
  2012. struct regmap *map = context;
  2013. return map->bus->reg_read(map->bus_context, reg, val);
  2014. }
  2015. static int _regmap_bus_read(void *context, unsigned int reg,
  2016. unsigned int *val)
  2017. {
  2018. int ret;
  2019. struct regmap *map = context;
  2020. void *work_val = map->work_buf + map->format.reg_bytes +
  2021. map->format.pad_bytes;
  2022. if (!map->format.parse_val)
  2023. return -EINVAL;
  2024. ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes);
  2025. if (ret == 0)
  2026. *val = map->format.parse_val(work_val);
  2027. return ret;
  2028. }
  2029. static int _regmap_read(struct regmap *map, unsigned int reg,
  2030. unsigned int *val)
  2031. {
  2032. int ret;
  2033. void *context = _regmap_map_get_context(map);
  2034. if (!map->cache_bypass) {
  2035. ret = regcache_read(map, reg, val);
  2036. if (ret == 0)
  2037. return 0;
  2038. }
  2039. if (map->cache_only)
  2040. return -EBUSY;
  2041. if (!regmap_readable(map, reg))
  2042. return -EIO;
  2043. ret = map->reg_read(context, reg, val);
  2044. if (ret == 0) {
  2045. #ifdef LOG_DEVICE
  2046. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  2047. dev_info(map->dev, "%x => %x\n", reg, *val);
  2048. #endif
  2049. trace_regmap_reg_read(map, reg, *val);
  2050. if (!map->cache_bypass)
  2051. regcache_write(map, reg, *val);
  2052. }
  2053. return ret;
  2054. }
  2055. /**
  2056. * regmap_read() - Read a value from a single register
  2057. *
  2058. * @map: Register map to read from
  2059. * @reg: Register to be read from
  2060. * @val: Pointer to store read value
  2061. *
  2062. * A value of zero will be returned on success, a negative errno will
  2063. * be returned in error cases.
  2064. */
  2065. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  2066. {
  2067. int ret;
  2068. if (!IS_ALIGNED(reg, map->reg_stride))
  2069. return -EINVAL;
  2070. map->lock(map->lock_arg);
  2071. ret = _regmap_read(map, reg, val);
  2072. map->unlock(map->lock_arg);
  2073. return ret;
  2074. }
  2075. EXPORT_SYMBOL_GPL(regmap_read);
  2076. /**
  2077. * regmap_raw_read() - Read raw data from the device
  2078. *
  2079. * @map: Register map to read from
  2080. * @reg: First register to be read from
  2081. * @val: Pointer to store read value
  2082. * @val_len: Size of data to read
  2083. *
  2084. * A value of zero will be returned on success, a negative errno will
  2085. * be returned in error cases.
  2086. */
  2087. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  2088. size_t val_len)
  2089. {
  2090. size_t val_bytes = map->format.val_bytes;
  2091. size_t val_count = val_len / val_bytes;
  2092. unsigned int v;
  2093. int ret, i;
  2094. if (!map->bus)
  2095. return -EINVAL;
  2096. if (val_len % map->format.val_bytes)
  2097. return -EINVAL;
  2098. if (!IS_ALIGNED(reg, map->reg_stride))
  2099. return -EINVAL;
  2100. if (val_count == 0)
  2101. return -EINVAL;
  2102. map->lock(map->lock_arg);
  2103. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  2104. map->cache_type == REGCACHE_NONE) {
  2105. size_t chunk_count, chunk_bytes;
  2106. size_t chunk_regs = val_count;
  2107. if (!map->bus->read) {
  2108. ret = -ENOTSUPP;
  2109. goto out;
  2110. }
  2111. if (map->use_single_read)
  2112. chunk_regs = 1;
  2113. else if (map->max_raw_read && val_len > map->max_raw_read)
  2114. chunk_regs = map->max_raw_read / val_bytes;
  2115. chunk_count = val_count / chunk_regs;
  2116. chunk_bytes = chunk_regs * val_bytes;
  2117. /* Read bytes that fit into whole chunks */
  2118. for (i = 0; i < chunk_count; i++) {
  2119. ret = _regmap_raw_read(map, reg, val, chunk_bytes);
  2120. if (ret != 0)
  2121. goto out;
  2122. reg += regmap_get_offset(map, chunk_regs);
  2123. val += chunk_bytes;
  2124. val_len -= chunk_bytes;
  2125. }
  2126. /* Read remaining bytes */
  2127. if (val_len) {
  2128. ret = _regmap_raw_read(map, reg, val, val_len);
  2129. if (ret != 0)
  2130. goto out;
  2131. }
  2132. } else {
  2133. /* Otherwise go word by word for the cache; should be low
  2134. * cost as we expect to hit the cache.
  2135. */
  2136. for (i = 0; i < val_count; i++) {
  2137. ret = _regmap_read(map, reg + regmap_get_offset(map, i),
  2138. &v);
  2139. if (ret != 0)
  2140. goto out;
  2141. map->format.format_val(val + (i * val_bytes), v, 0);
  2142. }
  2143. }
  2144. out:
  2145. map->unlock(map->lock_arg);
  2146. return ret;
  2147. }
  2148. EXPORT_SYMBOL_GPL(regmap_raw_read);
  2149. /**
  2150. * regmap_noinc_read(): Read data from a register without incrementing the
  2151. * register number
  2152. *
  2153. * @map: Register map to read from
  2154. * @reg: Register to read from
  2155. * @val: Pointer to data buffer
  2156. * @val_len: Length of output buffer in bytes.
  2157. *
  2158. * The regmap API usually assumes that bulk bus read operations will read a
  2159. * range of registers. Some devices have certain registers for which a read
  2160. * operation read will read from an internal FIFO.
  2161. *
  2162. * The target register must be volatile but registers after it can be
  2163. * completely unrelated cacheable registers.
  2164. *
  2165. * This will attempt multiple reads as required to read val_len bytes.
  2166. *
  2167. * A value of zero will be returned on success, a negative errno will be
  2168. * returned in error cases.
  2169. */
  2170. int regmap_noinc_read(struct regmap *map, unsigned int reg,
  2171. void *val, size_t val_len)
  2172. {
  2173. size_t read_len;
  2174. int ret;
  2175. if (!map->bus)
  2176. return -EINVAL;
  2177. if (!map->bus->read)
  2178. return -ENOTSUPP;
  2179. if (val_len % map->format.val_bytes)
  2180. return -EINVAL;
  2181. if (!IS_ALIGNED(reg, map->reg_stride))
  2182. return -EINVAL;
  2183. if (val_len == 0)
  2184. return -EINVAL;
  2185. map->lock(map->lock_arg);
  2186. if (!regmap_volatile(map, reg) || !regmap_readable_noinc(map, reg)) {
  2187. ret = -EINVAL;
  2188. goto out_unlock;
  2189. }
  2190. while (val_len) {
  2191. if (map->max_raw_read && map->max_raw_read < val_len)
  2192. read_len = map->max_raw_read;
  2193. else
  2194. read_len = val_len;
  2195. ret = _regmap_raw_read(map, reg, val, read_len);
  2196. if (ret)
  2197. goto out_unlock;
  2198. val = ((u8 *)val) + read_len;
  2199. val_len -= read_len;
  2200. }
  2201. out_unlock:
  2202. map->unlock(map->lock_arg);
  2203. return ret;
  2204. }
  2205. EXPORT_SYMBOL_GPL(regmap_noinc_read);
  2206. /**
  2207. * regmap_field_read(): Read a value to a single register field
  2208. *
  2209. * @field: Register field to read from
  2210. * @val: Pointer to store read value
  2211. *
  2212. * A value of zero will be returned on success, a negative errno will
  2213. * be returned in error cases.
  2214. */
  2215. int regmap_field_read(struct regmap_field *field, unsigned int *val)
  2216. {
  2217. int ret;
  2218. unsigned int reg_val;
  2219. ret = regmap_read(field->regmap, field->reg, &reg_val);
  2220. if (ret != 0)
  2221. return ret;
  2222. reg_val &= field->mask;
  2223. reg_val >>= field->shift;
  2224. *val = reg_val;
  2225. return ret;
  2226. }
  2227. EXPORT_SYMBOL_GPL(regmap_field_read);
  2228. /**
  2229. * regmap_fields_read() - Read a value to a single register field with port ID
  2230. *
  2231. * @field: Register field to read from
  2232. * @id: port ID
  2233. * @val: Pointer to store read value
  2234. *
  2235. * A value of zero will be returned on success, a negative errno will
  2236. * be returned in error cases.
  2237. */
  2238. int regmap_fields_read(struct regmap_field *field, unsigned int id,
  2239. unsigned int *val)
  2240. {
  2241. int ret;
  2242. unsigned int reg_val;
  2243. if (id >= field->id_size)
  2244. return -EINVAL;
  2245. ret = regmap_read(field->regmap,
  2246. field->reg + (field->id_offset * id),
  2247. &reg_val);
  2248. if (ret != 0)
  2249. return ret;
  2250. reg_val &= field->mask;
  2251. reg_val >>= field->shift;
  2252. *val = reg_val;
  2253. return ret;
  2254. }
  2255. EXPORT_SYMBOL_GPL(regmap_fields_read);
  2256. /**
  2257. * regmap_bulk_read() - Read multiple registers from the device
  2258. *
  2259. * @map: Register map to read from
  2260. * @reg: First register to be read from
  2261. * @val: Pointer to store read value, in native register size for device
  2262. * @val_count: Number of registers to read
  2263. *
  2264. * A value of zero will be returned on success, a negative errno will
  2265. * be returned in error cases.
  2266. */
  2267. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  2268. size_t val_count)
  2269. {
  2270. int ret, i;
  2271. size_t val_bytes = map->format.val_bytes;
  2272. bool vol = regmap_volatile_range(map, reg, val_count);
  2273. if (!IS_ALIGNED(reg, map->reg_stride))
  2274. return -EINVAL;
  2275. if (val_count == 0)
  2276. return -EINVAL;
  2277. if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
  2278. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  2279. if (ret != 0)
  2280. return ret;
  2281. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  2282. map->format.parse_inplace(val + i);
  2283. } else {
  2284. #ifdef CONFIG_64BIT
  2285. u64 *u64 = val;
  2286. #endif
  2287. u32 *u32 = val;
  2288. u16 *u16 = val;
  2289. u8 *u8 = val;
  2290. map->lock(map->lock_arg);
  2291. for (i = 0; i < val_count; i++) {
  2292. unsigned int ival;
  2293. ret = _regmap_read(map, reg + regmap_get_offset(map, i),
  2294. &ival);
  2295. if (ret != 0)
  2296. goto out;
  2297. switch (map->format.val_bytes) {
  2298. #ifdef CONFIG_64BIT
  2299. case 8:
  2300. u64[i] = ival;
  2301. break;
  2302. #endif
  2303. case 4:
  2304. u32[i] = ival;
  2305. break;
  2306. case 2:
  2307. u16[i] = ival;
  2308. break;
  2309. case 1:
  2310. u8[i] = ival;
  2311. break;
  2312. default:
  2313. ret = -EINVAL;
  2314. goto out;
  2315. }
  2316. }
  2317. out:
  2318. map->unlock(map->lock_arg);
  2319. }
  2320. return ret;
  2321. }
  2322. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  2323. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  2324. unsigned int mask, unsigned int val,
  2325. bool *change, bool force_write)
  2326. {
  2327. int ret;
  2328. unsigned int tmp, orig;
  2329. if (change)
  2330. *change = false;
  2331. if (regmap_volatile(map, reg) && map->reg_update_bits) {
  2332. ret = map->reg_update_bits(map->bus_context, reg, mask, val);
  2333. if (ret == 0 && change)
  2334. *change = true;
  2335. } else {
  2336. ret = _regmap_read(map, reg, &orig);
  2337. if (ret != 0)
  2338. return ret;
  2339. tmp = orig & ~mask;
  2340. tmp |= val & mask;
  2341. if (force_write || (tmp != orig)) {
  2342. ret = _regmap_write(map, reg, tmp);
  2343. if (ret == 0 && change)
  2344. *change = true;
  2345. }
  2346. }
  2347. return ret;
  2348. }
  2349. /**
  2350. * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
  2351. *
  2352. * @map: Register map to update
  2353. * @reg: Register to update
  2354. * @mask: Bitmask to change
  2355. * @val: New value for bitmask
  2356. * @change: Boolean indicating if a write was done
  2357. * @async: Boolean indicating asynchronously
  2358. * @force: Boolean indicating use force update
  2359. *
  2360. * Perform a read/modify/write cycle on a register map with change, async, force
  2361. * options.
  2362. *
  2363. * If async is true:
  2364. *
  2365. * With most buses the read must be done synchronously so this is most useful
  2366. * for devices with a cache which do not need to interact with the hardware to
  2367. * determine the current register value.
  2368. *
  2369. * Returns zero for success, a negative number on error.
  2370. */
  2371. int regmap_update_bits_base(struct regmap *map, unsigned int reg,
  2372. unsigned int mask, unsigned int val,
  2373. bool *change, bool async, bool force)
  2374. {
  2375. int ret;
  2376. map->lock(map->lock_arg);
  2377. map->async = async;
  2378. ret = _regmap_update_bits(map, reg, mask, val, change, force);
  2379. map->async = false;
  2380. map->unlock(map->lock_arg);
  2381. return ret;
  2382. }
  2383. EXPORT_SYMBOL_GPL(regmap_update_bits_base);
  2384. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  2385. {
  2386. struct regmap *map = async->map;
  2387. bool wake;
  2388. trace_regmap_async_io_complete(map);
  2389. spin_lock(&map->async_lock);
  2390. list_move(&async->list, &map->async_free);
  2391. wake = list_empty(&map->async_list);
  2392. if (ret != 0)
  2393. map->async_ret = ret;
  2394. spin_unlock(&map->async_lock);
  2395. if (wake)
  2396. wake_up(&map->async_waitq);
  2397. }
  2398. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  2399. static int regmap_async_is_done(struct regmap *map)
  2400. {
  2401. unsigned long flags;
  2402. int ret;
  2403. spin_lock_irqsave(&map->async_lock, flags);
  2404. ret = list_empty(&map->async_list);
  2405. spin_unlock_irqrestore(&map->async_lock, flags);
  2406. return ret;
  2407. }
  2408. /**
  2409. * regmap_async_complete - Ensure all asynchronous I/O has completed.
  2410. *
  2411. * @map: Map to operate on.
  2412. *
  2413. * Blocks until any pending asynchronous I/O has completed. Returns
  2414. * an error code for any failed I/O operations.
  2415. */
  2416. int regmap_async_complete(struct regmap *map)
  2417. {
  2418. unsigned long flags;
  2419. int ret;
  2420. /* Nothing to do with no async support */
  2421. if (!map->bus || !map->bus->async_write)
  2422. return 0;
  2423. trace_regmap_async_complete_start(map);
  2424. wait_event(map->async_waitq, regmap_async_is_done(map));
  2425. spin_lock_irqsave(&map->async_lock, flags);
  2426. ret = map->async_ret;
  2427. map->async_ret = 0;
  2428. spin_unlock_irqrestore(&map->async_lock, flags);
  2429. trace_regmap_async_complete_done(map);
  2430. return ret;
  2431. }
  2432. EXPORT_SYMBOL_GPL(regmap_async_complete);
  2433. /**
  2434. * regmap_register_patch - Register and apply register updates to be applied
  2435. * on device initialistion
  2436. *
  2437. * @map: Register map to apply updates to.
  2438. * @regs: Values to update.
  2439. * @num_regs: Number of entries in regs.
  2440. *
  2441. * Register a set of register updates to be applied to the device
  2442. * whenever the device registers are synchronised with the cache and
  2443. * apply them immediately. Typically this is used to apply
  2444. * corrections to be applied to the device defaults on startup, such
  2445. * as the updates some vendors provide to undocumented registers.
  2446. *
  2447. * The caller must ensure that this function cannot be called
  2448. * concurrently with either itself or regcache_sync().
  2449. */
  2450. int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
  2451. int num_regs)
  2452. {
  2453. struct reg_sequence *p;
  2454. int ret;
  2455. bool bypass;
  2456. if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
  2457. num_regs))
  2458. return 0;
  2459. p = krealloc(map->patch,
  2460. sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
  2461. GFP_KERNEL);
  2462. if (p) {
  2463. memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
  2464. map->patch = p;
  2465. map->patch_regs += num_regs;
  2466. } else {
  2467. return -ENOMEM;
  2468. }
  2469. map->lock(map->lock_arg);
  2470. bypass = map->cache_bypass;
  2471. map->cache_bypass = true;
  2472. map->async = true;
  2473. ret = _regmap_multi_reg_write(map, regs, num_regs);
  2474. map->async = false;
  2475. map->cache_bypass = bypass;
  2476. map->unlock(map->lock_arg);
  2477. regmap_async_complete(map);
  2478. return ret;
  2479. }
  2480. EXPORT_SYMBOL_GPL(regmap_register_patch);
  2481. /**
  2482. * regmap_get_val_bytes() - Report the size of a register value
  2483. *
  2484. * @map: Register map to operate on.
  2485. *
  2486. * Report the size of a register value, mainly intended to for use by
  2487. * generic infrastructure built on top of regmap.
  2488. */
  2489. int regmap_get_val_bytes(struct regmap *map)
  2490. {
  2491. if (map->format.format_write)
  2492. return -EINVAL;
  2493. return map->format.val_bytes;
  2494. }
  2495. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  2496. /**
  2497. * regmap_get_max_register() - Report the max register value
  2498. *
  2499. * @map: Register map to operate on.
  2500. *
  2501. * Report the max register value, mainly intended to for use by
  2502. * generic infrastructure built on top of regmap.
  2503. */
  2504. int regmap_get_max_register(struct regmap *map)
  2505. {
  2506. return map->max_register ? map->max_register : -EINVAL;
  2507. }
  2508. EXPORT_SYMBOL_GPL(regmap_get_max_register);
  2509. /**
  2510. * regmap_get_reg_stride() - Report the register address stride
  2511. *
  2512. * @map: Register map to operate on.
  2513. *
  2514. * Report the register address stride, mainly intended to for use by
  2515. * generic infrastructure built on top of regmap.
  2516. */
  2517. int regmap_get_reg_stride(struct regmap *map)
  2518. {
  2519. return map->reg_stride;
  2520. }
  2521. EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
  2522. int regmap_parse_val(struct regmap *map, const void *buf,
  2523. unsigned int *val)
  2524. {
  2525. if (!map->format.parse_val)
  2526. return -EINVAL;
  2527. *val = map->format.parse_val(buf);
  2528. return 0;
  2529. }
  2530. EXPORT_SYMBOL_GPL(regmap_parse_val);
  2531. static int __init regmap_initcall(void)
  2532. {
  2533. regmap_debugfs_initcall();
  2534. return 0;
  2535. }
  2536. postcore_initcall(regmap_initcall);