regmap.c 69 KB

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