regmap.c 70 KB

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