regmap.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  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. } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
  1237. /* regcache_drop_region() takes lock that we already have,
  1238. * thus call map->cache_ops->drop() directly
  1239. */
  1240. if (map->cache_ops && map->cache_ops->drop)
  1241. map->cache_ops->drop(map, reg, reg + 1);
  1242. }
  1243. trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
  1244. return ret;
  1245. }
  1246. /**
  1247. * regmap_can_raw_write - Test if regmap_raw_write() is supported
  1248. *
  1249. * @map: Map to check.
  1250. */
  1251. bool regmap_can_raw_write(struct regmap *map)
  1252. {
  1253. return map->bus && map->bus->write && map->format.format_val &&
  1254. map->format.format_reg;
  1255. }
  1256. EXPORT_SYMBOL_GPL(regmap_can_raw_write);
  1257. /**
  1258. * regmap_get_raw_read_max - Get the maximum size we can read
  1259. *
  1260. * @map: Map to check.
  1261. */
  1262. size_t regmap_get_raw_read_max(struct regmap *map)
  1263. {
  1264. return map->max_raw_read;
  1265. }
  1266. EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
  1267. /**
  1268. * regmap_get_raw_write_max - Get the maximum size we can read
  1269. *
  1270. * @map: Map to check.
  1271. */
  1272. size_t regmap_get_raw_write_max(struct regmap *map)
  1273. {
  1274. return map->max_raw_write;
  1275. }
  1276. EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
  1277. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  1278. unsigned int val)
  1279. {
  1280. int ret;
  1281. struct regmap_range_node *range;
  1282. struct regmap *map = context;
  1283. WARN_ON(!map->bus || !map->format.format_write);
  1284. range = _regmap_range_lookup(map, reg);
  1285. if (range) {
  1286. ret = _regmap_select_page(map, &reg, range, 1);
  1287. if (ret != 0)
  1288. return ret;
  1289. }
  1290. map->format.format_write(map, reg, val);
  1291. trace_regmap_hw_write_start(map, reg, 1);
  1292. ret = map->bus->write(map->bus_context, map->work_buf,
  1293. map->format.buf_size);
  1294. trace_regmap_hw_write_done(map, reg, 1);
  1295. return ret;
  1296. }
  1297. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  1298. unsigned int val)
  1299. {
  1300. struct regmap *map = context;
  1301. return map->bus->reg_write(map->bus_context, reg, val);
  1302. }
  1303. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  1304. unsigned int val)
  1305. {
  1306. struct regmap *map = context;
  1307. WARN_ON(!map->bus || !map->format.format_val);
  1308. map->format.format_val(map->work_buf + map->format.reg_bytes
  1309. + map->format.pad_bytes, val, 0);
  1310. return _regmap_raw_write(map, reg,
  1311. map->work_buf +
  1312. map->format.reg_bytes +
  1313. map->format.pad_bytes,
  1314. map->format.val_bytes);
  1315. }
  1316. static inline void *_regmap_map_get_context(struct regmap *map)
  1317. {
  1318. return (map->bus) ? map : map->bus_context;
  1319. }
  1320. int _regmap_write(struct regmap *map, unsigned int reg,
  1321. unsigned int val)
  1322. {
  1323. int ret;
  1324. void *context = _regmap_map_get_context(map);
  1325. if (!regmap_writeable(map, reg))
  1326. return -EIO;
  1327. if (!map->cache_bypass && !map->defer_caching) {
  1328. ret = regcache_write(map, reg, val);
  1329. if (ret != 0)
  1330. return ret;
  1331. if (map->cache_only) {
  1332. map->cache_dirty = true;
  1333. return 0;
  1334. }
  1335. }
  1336. #ifdef LOG_DEVICE
  1337. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1338. dev_info(map->dev, "%x <= %x\n", reg, val);
  1339. #endif
  1340. trace_regmap_reg_write(map, reg, val);
  1341. return map->reg_write(context, reg, val);
  1342. }
  1343. /**
  1344. * regmap_write(): Write a value to a single register
  1345. *
  1346. * @map: Register map to write to
  1347. * @reg: Register to write to
  1348. * @val: Value to be written
  1349. *
  1350. * A value of zero will be returned on success, a negative errno will
  1351. * be returned in error cases.
  1352. */
  1353. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  1354. {
  1355. int ret;
  1356. if (!IS_ALIGNED(reg, map->reg_stride))
  1357. return -EINVAL;
  1358. map->lock(map->lock_arg);
  1359. ret = _regmap_write(map, reg, val);
  1360. map->unlock(map->lock_arg);
  1361. return ret;
  1362. }
  1363. EXPORT_SYMBOL_GPL(regmap_write);
  1364. /**
  1365. * regmap_write_async(): Write a value to a single register asynchronously
  1366. *
  1367. * @map: Register map to write to
  1368. * @reg: Register to write to
  1369. * @val: Value to be written
  1370. *
  1371. * A value of zero will be returned on success, a negative errno will
  1372. * be returned in error cases.
  1373. */
  1374. int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
  1375. {
  1376. int ret;
  1377. if (!IS_ALIGNED(reg, map->reg_stride))
  1378. return -EINVAL;
  1379. map->lock(map->lock_arg);
  1380. map->async = true;
  1381. ret = _regmap_write(map, reg, val);
  1382. map->async = false;
  1383. map->unlock(map->lock_arg);
  1384. return ret;
  1385. }
  1386. EXPORT_SYMBOL_GPL(regmap_write_async);
  1387. /**
  1388. * regmap_raw_write(): Write raw values to one or more registers
  1389. *
  1390. * @map: Register map to write to
  1391. * @reg: Initial register to write to
  1392. * @val: Block of data to be written, laid out for direct transmission to the
  1393. * device
  1394. * @val_len: Length of data pointed to by val.
  1395. *
  1396. * This function is intended to be used for things like firmware
  1397. * download where a large block of data needs to be transferred to the
  1398. * device. No formatting will be done on the data provided.
  1399. *
  1400. * A value of zero will be returned on success, a negative errno will
  1401. * be returned in error cases.
  1402. */
  1403. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1404. const void *val, size_t val_len)
  1405. {
  1406. int ret;
  1407. if (!regmap_can_raw_write(map))
  1408. return -EINVAL;
  1409. if (val_len % map->format.val_bytes)
  1410. return -EINVAL;
  1411. if (map->max_raw_write && map->max_raw_write > val_len)
  1412. return -E2BIG;
  1413. map->lock(map->lock_arg);
  1414. ret = _regmap_raw_write(map, reg, val, val_len);
  1415. map->unlock(map->lock_arg);
  1416. return ret;
  1417. }
  1418. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1419. /**
  1420. * regmap_field_update_bits_base():
  1421. * Perform a read/modify/write cycle on the register field
  1422. * with change, async, force option
  1423. *
  1424. * @field: Register field to write to
  1425. * @mask: Bitmask to change
  1426. * @val: Value to be written
  1427. * @change: Boolean indicating if a write was done
  1428. * @async: Boolean indicating asynchronously
  1429. * @force: Boolean indicating use force update
  1430. *
  1431. * A value of zero will be returned on success, a negative errno will
  1432. * be returned in error cases.
  1433. */
  1434. int regmap_field_update_bits_base(struct regmap_field *field,
  1435. unsigned int mask, unsigned int val,
  1436. bool *change, bool async, bool force)
  1437. {
  1438. mask = (mask << field->shift) & field->mask;
  1439. return regmap_update_bits_base(field->regmap, field->reg,
  1440. mask, val << field->shift,
  1441. change, async, force);
  1442. }
  1443. EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
  1444. /**
  1445. * regmap_fields_update_bits_base():
  1446. * Perform a read/modify/write cycle on the register field
  1447. * with change, async, force option
  1448. *
  1449. * @field: Register field to write to
  1450. * @id: port ID
  1451. * @mask: Bitmask to change
  1452. * @val: Value to be written
  1453. * @change: Boolean indicating if a write was done
  1454. * @async: Boolean indicating asynchronously
  1455. * @force: Boolean indicating use force update
  1456. *
  1457. * A value of zero will be returned on success, a negative errno will
  1458. * be returned in error cases.
  1459. */
  1460. int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
  1461. unsigned int mask, unsigned int val,
  1462. bool *change, bool async, bool force)
  1463. {
  1464. if (id >= field->id_size)
  1465. return -EINVAL;
  1466. mask = (mask << field->shift) & field->mask;
  1467. return regmap_update_bits_base(field->regmap,
  1468. field->reg + (field->id_offset * id),
  1469. mask, val << field->shift,
  1470. change, async, force);
  1471. }
  1472. EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
  1473. /*
  1474. * regmap_bulk_write(): Write multiple registers to the device
  1475. *
  1476. * @map: Register map to write to
  1477. * @reg: First register to be write from
  1478. * @val: Block of data to be written, in native register size for device
  1479. * @val_count: Number of registers to write
  1480. *
  1481. * This function is intended to be used for writing a large block of
  1482. * data to the device either in single transfer or multiple transfer.
  1483. *
  1484. * A value of zero will be returned on success, a negative errno will
  1485. * be returned in error cases.
  1486. */
  1487. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1488. size_t val_count)
  1489. {
  1490. int ret = 0, i;
  1491. size_t val_bytes = map->format.val_bytes;
  1492. size_t total_size = val_bytes * val_count;
  1493. if (!IS_ALIGNED(reg, map->reg_stride))
  1494. return -EINVAL;
  1495. /*
  1496. * Some devices don't support bulk write, for
  1497. * them we have a series of single write operations in the first two if
  1498. * blocks.
  1499. *
  1500. * The first if block is used for memory mapped io. It does not allow
  1501. * val_bytes of 3 for example.
  1502. * The second one is for busses that do not provide raw I/O.
  1503. * The third one is used for busses which do not have these limitations
  1504. * and can write arbitrary value lengths.
  1505. */
  1506. if (!map->bus) {
  1507. map->lock(map->lock_arg);
  1508. for (i = 0; i < val_count; i++) {
  1509. unsigned int ival;
  1510. switch (val_bytes) {
  1511. case 1:
  1512. ival = *(u8 *)(val + (i * val_bytes));
  1513. break;
  1514. case 2:
  1515. ival = *(u16 *)(val + (i * val_bytes));
  1516. break;
  1517. case 4:
  1518. ival = *(u32 *)(val + (i * val_bytes));
  1519. break;
  1520. #ifdef CONFIG_64BIT
  1521. case 8:
  1522. ival = *(u64 *)(val + (i * val_bytes));
  1523. break;
  1524. #endif
  1525. default:
  1526. ret = -EINVAL;
  1527. goto out;
  1528. }
  1529. ret = _regmap_write(map,
  1530. reg + regmap_get_offset(map, i),
  1531. ival);
  1532. if (ret != 0)
  1533. goto out;
  1534. }
  1535. out:
  1536. map->unlock(map->lock_arg);
  1537. } else if (map->bus && !map->format.parse_inplace) {
  1538. const u8 *u8 = val;
  1539. const u16 *u16 = val;
  1540. const u32 *u32 = val;
  1541. unsigned int ival;
  1542. for (i = 0; i < val_count; i++) {
  1543. switch (map->format.val_bytes) {
  1544. case 4:
  1545. ival = u32[i];
  1546. break;
  1547. case 2:
  1548. ival = u16[i];
  1549. break;
  1550. case 1:
  1551. ival = u8[i];
  1552. break;
  1553. default:
  1554. return -EINVAL;
  1555. }
  1556. ret = regmap_write(map, reg + (i * map->reg_stride),
  1557. ival);
  1558. if (ret)
  1559. return ret;
  1560. }
  1561. } else if (map->use_single_write ||
  1562. (map->max_raw_write && map->max_raw_write < total_size)) {
  1563. int chunk_stride = map->reg_stride;
  1564. size_t chunk_size = val_bytes;
  1565. size_t chunk_count = val_count;
  1566. if (!map->use_single_write) {
  1567. chunk_size = map->max_raw_write;
  1568. if (chunk_size % val_bytes)
  1569. chunk_size -= chunk_size % val_bytes;
  1570. chunk_count = total_size / chunk_size;
  1571. chunk_stride *= chunk_size / val_bytes;
  1572. }
  1573. map->lock(map->lock_arg);
  1574. /* Write as many bytes as possible with chunk_size */
  1575. for (i = 0; i < chunk_count; i++) {
  1576. ret = _regmap_raw_write(map,
  1577. reg + (i * chunk_stride),
  1578. val + (i * chunk_size),
  1579. chunk_size);
  1580. if (ret)
  1581. break;
  1582. }
  1583. /* Write remaining bytes */
  1584. if (!ret && chunk_size * i < total_size) {
  1585. ret = _regmap_raw_write(map, reg + (i * chunk_stride),
  1586. val + (i * chunk_size),
  1587. total_size - i * chunk_size);
  1588. }
  1589. map->unlock(map->lock_arg);
  1590. } else {
  1591. void *wval;
  1592. if (!val_count)
  1593. return -EINVAL;
  1594. wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
  1595. if (!wval) {
  1596. dev_err(map->dev, "Error in memory allocation\n");
  1597. return -ENOMEM;
  1598. }
  1599. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1600. map->format.parse_inplace(wval + i);
  1601. map->lock(map->lock_arg);
  1602. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
  1603. map->unlock(map->lock_arg);
  1604. kfree(wval);
  1605. }
  1606. return ret;
  1607. }
  1608. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1609. /*
  1610. * _regmap_raw_multi_reg_write()
  1611. *
  1612. * the (register,newvalue) pairs in regs have not been formatted, but
  1613. * they are all in the same page and have been changed to being page
  1614. * relative. The page register has been written if that was necessary.
  1615. */
  1616. static int _regmap_raw_multi_reg_write(struct regmap *map,
  1617. const struct reg_sequence *regs,
  1618. size_t num_regs)
  1619. {
  1620. int ret;
  1621. void *buf;
  1622. int i;
  1623. u8 *u8;
  1624. size_t val_bytes = map->format.val_bytes;
  1625. size_t reg_bytes = map->format.reg_bytes;
  1626. size_t pad_bytes = map->format.pad_bytes;
  1627. size_t pair_size = reg_bytes + pad_bytes + val_bytes;
  1628. size_t len = pair_size * num_regs;
  1629. if (!len)
  1630. return -EINVAL;
  1631. buf = kzalloc(len, GFP_KERNEL);
  1632. if (!buf)
  1633. return -ENOMEM;
  1634. /* We have to linearise by hand. */
  1635. u8 = buf;
  1636. for (i = 0; i < num_regs; i++) {
  1637. unsigned int reg = regs[i].reg;
  1638. unsigned int val = regs[i].def;
  1639. trace_regmap_hw_write_start(map, reg, 1);
  1640. map->format.format_reg(u8, reg, map->reg_shift);
  1641. u8 += reg_bytes + pad_bytes;
  1642. map->format.format_val(u8, val, 0);
  1643. u8 += val_bytes;
  1644. }
  1645. u8 = buf;
  1646. *u8 |= map->write_flag_mask;
  1647. ret = map->bus->write(map->bus_context, buf, len);
  1648. kfree(buf);
  1649. for (i = 0; i < num_regs; i++) {
  1650. int reg = regs[i].reg;
  1651. trace_regmap_hw_write_done(map, reg, 1);
  1652. }
  1653. return ret;
  1654. }
  1655. static unsigned int _regmap_register_page(struct regmap *map,
  1656. unsigned int reg,
  1657. struct regmap_range_node *range)
  1658. {
  1659. unsigned int win_page = (reg - range->range_min) / range->window_len;
  1660. return win_page;
  1661. }
  1662. static int _regmap_range_multi_paged_reg_write(struct regmap *map,
  1663. struct reg_sequence *regs,
  1664. size_t num_regs)
  1665. {
  1666. int ret;
  1667. int i, n;
  1668. struct reg_sequence *base;
  1669. unsigned int this_page = 0;
  1670. unsigned int page_change = 0;
  1671. /*
  1672. * the set of registers are not neccessarily in order, but
  1673. * since the order of write must be preserved this algorithm
  1674. * chops the set each time the page changes. This also applies
  1675. * if there is a delay required at any point in the sequence.
  1676. */
  1677. base = regs;
  1678. for (i = 0, n = 0; i < num_regs; i++, n++) {
  1679. unsigned int reg = regs[i].reg;
  1680. struct regmap_range_node *range;
  1681. range = _regmap_range_lookup(map, reg);
  1682. if (range) {
  1683. unsigned int win_page = _regmap_register_page(map, reg,
  1684. range);
  1685. if (i == 0)
  1686. this_page = win_page;
  1687. if (win_page != this_page) {
  1688. this_page = win_page;
  1689. page_change = 1;
  1690. }
  1691. }
  1692. /* If we have both a page change and a delay make sure to
  1693. * write the regs and apply the delay before we change the
  1694. * page.
  1695. */
  1696. if (page_change || regs[i].delay_us) {
  1697. /* For situations where the first write requires
  1698. * a delay we need to make sure we don't call
  1699. * raw_multi_reg_write with n=0
  1700. * This can't occur with page breaks as we
  1701. * never write on the first iteration
  1702. */
  1703. if (regs[i].delay_us && i == 0)
  1704. n = 1;
  1705. ret = _regmap_raw_multi_reg_write(map, base, n);
  1706. if (ret != 0)
  1707. return ret;
  1708. if (regs[i].delay_us)
  1709. udelay(regs[i].delay_us);
  1710. base += n;
  1711. n = 0;
  1712. if (page_change) {
  1713. ret = _regmap_select_page(map,
  1714. &base[n].reg,
  1715. range, 1);
  1716. if (ret != 0)
  1717. return ret;
  1718. page_change = 0;
  1719. }
  1720. }
  1721. }
  1722. if (n > 0)
  1723. return _regmap_raw_multi_reg_write(map, base, n);
  1724. return 0;
  1725. }
  1726. static int _regmap_multi_reg_write(struct regmap *map,
  1727. const struct reg_sequence *regs,
  1728. size_t num_regs)
  1729. {
  1730. int i;
  1731. int ret;
  1732. if (!map->can_multi_write) {
  1733. for (i = 0; i < num_regs; i++) {
  1734. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1735. if (ret != 0)
  1736. return ret;
  1737. if (regs[i].delay_us)
  1738. udelay(regs[i].delay_us);
  1739. }
  1740. return 0;
  1741. }
  1742. if (!map->format.parse_inplace)
  1743. return -EINVAL;
  1744. if (map->writeable_reg)
  1745. for (i = 0; i < num_regs; i++) {
  1746. int reg = regs[i].reg;
  1747. if (!map->writeable_reg(map->dev, reg))
  1748. return -EINVAL;
  1749. if (!IS_ALIGNED(reg, map->reg_stride))
  1750. return -EINVAL;
  1751. }
  1752. if (!map->cache_bypass) {
  1753. for (i = 0; i < num_regs; i++) {
  1754. unsigned int val = regs[i].def;
  1755. unsigned int reg = regs[i].reg;
  1756. ret = regcache_write(map, reg, val);
  1757. if (ret) {
  1758. dev_err(map->dev,
  1759. "Error in caching of register: %x ret: %d\n",
  1760. reg, ret);
  1761. return ret;
  1762. }
  1763. }
  1764. if (map->cache_only) {
  1765. map->cache_dirty = true;
  1766. return 0;
  1767. }
  1768. }
  1769. WARN_ON(!map->bus);
  1770. for (i = 0; i < num_regs; i++) {
  1771. unsigned int reg = regs[i].reg;
  1772. struct regmap_range_node *range;
  1773. /* Coalesce all the writes between a page break or a delay
  1774. * in a sequence
  1775. */
  1776. range = _regmap_range_lookup(map, reg);
  1777. if (range || regs[i].delay_us) {
  1778. size_t len = sizeof(struct reg_sequence)*num_regs;
  1779. struct reg_sequence *base = kmemdup(regs, len,
  1780. GFP_KERNEL);
  1781. if (!base)
  1782. return -ENOMEM;
  1783. ret = _regmap_range_multi_paged_reg_write(map, base,
  1784. num_regs);
  1785. kfree(base);
  1786. return ret;
  1787. }
  1788. }
  1789. return _regmap_raw_multi_reg_write(map, regs, num_regs);
  1790. }
  1791. /*
  1792. * regmap_multi_reg_write(): Write multiple registers to the device
  1793. *
  1794. * where the set of register,value pairs are supplied in any order,
  1795. * possibly not all in a single range.
  1796. *
  1797. * @map: Register map to write to
  1798. * @regs: Array of structures containing register,value to be written
  1799. * @num_regs: Number of registers to write
  1800. *
  1801. * The 'normal' block write mode will send ultimately send data on the
  1802. * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
  1803. * addressed. However, this alternative block multi write mode will send
  1804. * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
  1805. * must of course support the mode.
  1806. *
  1807. * A value of zero will be returned on success, a negative errno will be
  1808. * returned in error cases.
  1809. */
  1810. int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
  1811. int num_regs)
  1812. {
  1813. int ret;
  1814. map->lock(map->lock_arg);
  1815. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1816. map->unlock(map->lock_arg);
  1817. return ret;
  1818. }
  1819. EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
  1820. /*
  1821. * regmap_multi_reg_write_bypassed(): Write multiple registers to the
  1822. * device but not the cache
  1823. *
  1824. * where the set of register are supplied in any order
  1825. *
  1826. * @map: Register map to write to
  1827. * @regs: Array of structures containing register,value to be written
  1828. * @num_regs: Number of registers to write
  1829. *
  1830. * This function is intended to be used for writing a large block of data
  1831. * atomically to the device in single transfer for those I2C client devices
  1832. * that implement this alternative block write mode.
  1833. *
  1834. * A value of zero will be returned on success, a negative errno will
  1835. * be returned in error cases.
  1836. */
  1837. int regmap_multi_reg_write_bypassed(struct regmap *map,
  1838. const struct reg_sequence *regs,
  1839. int num_regs)
  1840. {
  1841. int ret;
  1842. bool bypass;
  1843. map->lock(map->lock_arg);
  1844. bypass = map->cache_bypass;
  1845. map->cache_bypass = true;
  1846. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1847. map->cache_bypass = bypass;
  1848. map->unlock(map->lock_arg);
  1849. return ret;
  1850. }
  1851. EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
  1852. /**
  1853. * regmap_raw_write_async(): Write raw values to one or more registers
  1854. * asynchronously
  1855. *
  1856. * @map: Register map to write to
  1857. * @reg: Initial register to write to
  1858. * @val: Block of data to be written, laid out for direct transmission to the
  1859. * device. Must be valid until regmap_async_complete() is called.
  1860. * @val_len: Length of data pointed to by val.
  1861. *
  1862. * This function is intended to be used for things like firmware
  1863. * download where a large block of data needs to be transferred to the
  1864. * device. No formatting will be done on the data provided.
  1865. *
  1866. * If supported by the underlying bus the write will be scheduled
  1867. * asynchronously, helping maximise I/O speed on higher speed buses
  1868. * like SPI. regmap_async_complete() can be called to ensure that all
  1869. * asynchrnous writes have been completed.
  1870. *
  1871. * A value of zero will be returned on success, a negative errno will
  1872. * be returned in error cases.
  1873. */
  1874. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  1875. const void *val, size_t val_len)
  1876. {
  1877. int ret;
  1878. if (val_len % map->format.val_bytes)
  1879. return -EINVAL;
  1880. if (!IS_ALIGNED(reg, map->reg_stride))
  1881. return -EINVAL;
  1882. map->lock(map->lock_arg);
  1883. map->async = true;
  1884. ret = _regmap_raw_write(map, reg, val, val_len);
  1885. map->async = false;
  1886. map->unlock(map->lock_arg);
  1887. return ret;
  1888. }
  1889. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  1890. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1891. unsigned int val_len)
  1892. {
  1893. struct regmap_range_node *range;
  1894. u8 *u8 = map->work_buf;
  1895. int ret;
  1896. WARN_ON(!map->bus);
  1897. if (!map->bus || !map->bus->read)
  1898. return -EINVAL;
  1899. range = _regmap_range_lookup(map, reg);
  1900. if (range) {
  1901. ret = _regmap_select_page(map, &reg, range,
  1902. val_len / map->format.val_bytes);
  1903. if (ret != 0)
  1904. return ret;
  1905. }
  1906. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1907. /*
  1908. * Some buses or devices flag reads by setting the high bits in the
  1909. * register address; since it's always the high bits for all
  1910. * current formats we can do this here rather than in
  1911. * formatting. This may break if we get interesting formats.
  1912. */
  1913. u8[0] |= map->read_flag_mask;
  1914. trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
  1915. ret = map->bus->read(map->bus_context, map->work_buf,
  1916. map->format.reg_bytes + map->format.pad_bytes,
  1917. val, val_len);
  1918. trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
  1919. return ret;
  1920. }
  1921. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  1922. unsigned int *val)
  1923. {
  1924. struct regmap *map = context;
  1925. return map->bus->reg_read(map->bus_context, reg, val);
  1926. }
  1927. static int _regmap_bus_read(void *context, unsigned int reg,
  1928. unsigned int *val)
  1929. {
  1930. int ret;
  1931. struct regmap *map = context;
  1932. if (!map->format.parse_val)
  1933. return -EINVAL;
  1934. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  1935. if (ret == 0)
  1936. *val = map->format.parse_val(map->work_buf);
  1937. return ret;
  1938. }
  1939. static int _regmap_read(struct regmap *map, unsigned int reg,
  1940. unsigned int *val)
  1941. {
  1942. int ret;
  1943. void *context = _regmap_map_get_context(map);
  1944. if (!map->cache_bypass) {
  1945. ret = regcache_read(map, reg, val);
  1946. if (ret == 0)
  1947. return 0;
  1948. }
  1949. if (map->cache_only)
  1950. return -EBUSY;
  1951. if (!regmap_readable(map, reg))
  1952. return -EIO;
  1953. ret = map->reg_read(context, reg, val);
  1954. if (ret == 0) {
  1955. #ifdef LOG_DEVICE
  1956. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1957. dev_info(map->dev, "%x => %x\n", reg, *val);
  1958. #endif
  1959. trace_regmap_reg_read(map, reg, *val);
  1960. if (!map->cache_bypass)
  1961. regcache_write(map, reg, *val);
  1962. }
  1963. return ret;
  1964. }
  1965. /**
  1966. * regmap_read(): Read a value from a single register
  1967. *
  1968. * @map: Register map to read from
  1969. * @reg: Register to be read from
  1970. * @val: Pointer to store read value
  1971. *
  1972. * A value of zero will be returned on success, a negative errno will
  1973. * be returned in error cases.
  1974. */
  1975. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  1976. {
  1977. int ret;
  1978. if (!IS_ALIGNED(reg, map->reg_stride))
  1979. return -EINVAL;
  1980. map->lock(map->lock_arg);
  1981. ret = _regmap_read(map, reg, val);
  1982. map->unlock(map->lock_arg);
  1983. return ret;
  1984. }
  1985. EXPORT_SYMBOL_GPL(regmap_read);
  1986. /**
  1987. * regmap_raw_read(): Read raw data from the device
  1988. *
  1989. * @map: Register map to read from
  1990. * @reg: First register to be read from
  1991. * @val: Pointer to store read value
  1992. * @val_len: Size of data to read
  1993. *
  1994. * A value of zero will be returned on success, a negative errno will
  1995. * be returned in error cases.
  1996. */
  1997. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1998. size_t val_len)
  1999. {
  2000. size_t val_bytes = map->format.val_bytes;
  2001. size_t val_count = val_len / val_bytes;
  2002. unsigned int v;
  2003. int ret, i;
  2004. if (!map->bus)
  2005. return -EINVAL;
  2006. if (val_len % map->format.val_bytes)
  2007. return -EINVAL;
  2008. if (!IS_ALIGNED(reg, map->reg_stride))
  2009. return -EINVAL;
  2010. if (val_count == 0)
  2011. return -EINVAL;
  2012. map->lock(map->lock_arg);
  2013. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  2014. map->cache_type == REGCACHE_NONE) {
  2015. if (!map->bus->read) {
  2016. ret = -ENOTSUPP;
  2017. goto out;
  2018. }
  2019. if (map->max_raw_read && map->max_raw_read < val_len) {
  2020. ret = -E2BIG;
  2021. goto out;
  2022. }
  2023. /* Physical block read if there's no cache involved */
  2024. ret = _regmap_raw_read(map, reg, val, val_len);
  2025. } else {
  2026. /* Otherwise go word by word for the cache; should be low
  2027. * cost as we expect to hit the cache.
  2028. */
  2029. for (i = 0; i < val_count; i++) {
  2030. ret = _regmap_read(map, reg + regmap_get_offset(map, i),
  2031. &v);
  2032. if (ret != 0)
  2033. goto out;
  2034. map->format.format_val(val + (i * val_bytes), v, 0);
  2035. }
  2036. }
  2037. out:
  2038. map->unlock(map->lock_arg);
  2039. return ret;
  2040. }
  2041. EXPORT_SYMBOL_GPL(regmap_raw_read);
  2042. /**
  2043. * regmap_field_read(): Read a value to a single register field
  2044. *
  2045. * @field: Register field to read from
  2046. * @val: Pointer to store read value
  2047. *
  2048. * A value of zero will be returned on success, a negative errno will
  2049. * be returned in error cases.
  2050. */
  2051. int regmap_field_read(struct regmap_field *field, unsigned int *val)
  2052. {
  2053. int ret;
  2054. unsigned int reg_val;
  2055. ret = regmap_read(field->regmap, field->reg, &reg_val);
  2056. if (ret != 0)
  2057. return ret;
  2058. reg_val &= field->mask;
  2059. reg_val >>= field->shift;
  2060. *val = reg_val;
  2061. return ret;
  2062. }
  2063. EXPORT_SYMBOL_GPL(regmap_field_read);
  2064. /**
  2065. * regmap_fields_read(): Read a value to a single register field with port ID
  2066. *
  2067. * @field: Register field to read from
  2068. * @id: port ID
  2069. * @val: Pointer to store read value
  2070. *
  2071. * A value of zero will be returned on success, a negative errno will
  2072. * be returned in error cases.
  2073. */
  2074. int regmap_fields_read(struct regmap_field *field, unsigned int id,
  2075. unsigned int *val)
  2076. {
  2077. int ret;
  2078. unsigned int reg_val;
  2079. if (id >= field->id_size)
  2080. return -EINVAL;
  2081. ret = regmap_read(field->regmap,
  2082. field->reg + (field->id_offset * id),
  2083. &reg_val);
  2084. if (ret != 0)
  2085. return ret;
  2086. reg_val &= field->mask;
  2087. reg_val >>= field->shift;
  2088. *val = reg_val;
  2089. return ret;
  2090. }
  2091. EXPORT_SYMBOL_GPL(regmap_fields_read);
  2092. /**
  2093. * regmap_bulk_read(): Read multiple registers from the device
  2094. *
  2095. * @map: Register map to read from
  2096. * @reg: First register to be read from
  2097. * @val: Pointer to store read value, in native register size for device
  2098. * @val_count: Number of registers to read
  2099. *
  2100. * A value of zero will be returned on success, a negative errno will
  2101. * be returned in error cases.
  2102. */
  2103. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  2104. size_t val_count)
  2105. {
  2106. int ret, i;
  2107. size_t val_bytes = map->format.val_bytes;
  2108. bool vol = regmap_volatile_range(map, reg, val_count);
  2109. if (!IS_ALIGNED(reg, map->reg_stride))
  2110. return -EINVAL;
  2111. if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
  2112. /*
  2113. * Some devices does not support bulk read, for
  2114. * them we have a series of single read operations.
  2115. */
  2116. size_t total_size = val_bytes * val_count;
  2117. if (!map->use_single_read &&
  2118. (!map->max_raw_read || map->max_raw_read > total_size)) {
  2119. ret = regmap_raw_read(map, reg, val,
  2120. val_bytes * val_count);
  2121. if (ret != 0)
  2122. return ret;
  2123. } else {
  2124. /*
  2125. * Some devices do not support bulk read or do not
  2126. * support large bulk reads, for them we have a series
  2127. * of read operations.
  2128. */
  2129. int chunk_stride = map->reg_stride;
  2130. size_t chunk_size = val_bytes;
  2131. size_t chunk_count = val_count;
  2132. if (!map->use_single_read) {
  2133. chunk_size = map->max_raw_read;
  2134. if (chunk_size % val_bytes)
  2135. chunk_size -= chunk_size % val_bytes;
  2136. chunk_count = total_size / chunk_size;
  2137. chunk_stride *= chunk_size / val_bytes;
  2138. }
  2139. /* Read bytes that fit into a multiple of chunk_size */
  2140. for (i = 0; i < chunk_count; i++) {
  2141. ret = regmap_raw_read(map,
  2142. reg + (i * chunk_stride),
  2143. val + (i * chunk_size),
  2144. chunk_size);
  2145. if (ret != 0)
  2146. return ret;
  2147. }
  2148. /* Read remaining bytes */
  2149. if (chunk_size * i < total_size) {
  2150. ret = regmap_raw_read(map,
  2151. reg + (i * chunk_stride),
  2152. val + (i * chunk_size),
  2153. total_size - i * chunk_size);
  2154. if (ret != 0)
  2155. return ret;
  2156. }
  2157. }
  2158. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  2159. map->format.parse_inplace(val + i);
  2160. } else {
  2161. for (i = 0; i < val_count; i++) {
  2162. unsigned int ival;
  2163. ret = regmap_read(map, reg + regmap_get_offset(map, i),
  2164. &ival);
  2165. if (ret != 0)
  2166. return ret;
  2167. if (map->format.format_val) {
  2168. map->format.format_val(val + (i * val_bytes), ival, 0);
  2169. } else {
  2170. /* Devices providing read and write
  2171. * operations can use the bulk I/O
  2172. * functions if they define a val_bytes,
  2173. * we assume that the values are native
  2174. * endian.
  2175. */
  2176. #ifdef CONFIG_64BIT
  2177. u64 *u64 = val;
  2178. #endif
  2179. u32 *u32 = val;
  2180. u16 *u16 = val;
  2181. u8 *u8 = val;
  2182. switch (map->format.val_bytes) {
  2183. #ifdef CONFIG_64BIT
  2184. case 8:
  2185. u64[i] = ival;
  2186. break;
  2187. #endif
  2188. case 4:
  2189. u32[i] = ival;
  2190. break;
  2191. case 2:
  2192. u16[i] = ival;
  2193. break;
  2194. case 1:
  2195. u8[i] = ival;
  2196. break;
  2197. default:
  2198. return -EINVAL;
  2199. }
  2200. }
  2201. }
  2202. }
  2203. return 0;
  2204. }
  2205. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  2206. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  2207. unsigned int mask, unsigned int val,
  2208. bool *change, bool force_write)
  2209. {
  2210. int ret;
  2211. unsigned int tmp, orig;
  2212. if (change)
  2213. *change = false;
  2214. if (regmap_volatile(map, reg) && map->reg_update_bits) {
  2215. ret = map->reg_update_bits(map->bus_context, reg, mask, val);
  2216. if (ret == 0 && change)
  2217. *change = true;
  2218. } else {
  2219. ret = _regmap_read(map, reg, &orig);
  2220. if (ret != 0)
  2221. return ret;
  2222. tmp = orig & ~mask;
  2223. tmp |= val & mask;
  2224. if (force_write || (tmp != orig)) {
  2225. ret = _regmap_write(map, reg, tmp);
  2226. if (ret == 0 && change)
  2227. *change = true;
  2228. }
  2229. }
  2230. return ret;
  2231. }
  2232. /**
  2233. * regmap_update_bits_base:
  2234. * Perform a read/modify/write cycle on the
  2235. * register map with change, async, force option
  2236. *
  2237. * @map: Register map to update
  2238. * @reg: Register to update
  2239. * @mask: Bitmask to change
  2240. * @val: New value for bitmask
  2241. * @change: Boolean indicating if a write was done
  2242. * @async: Boolean indicating asynchronously
  2243. * @force: Boolean indicating use force update
  2244. *
  2245. * if async was true,
  2246. * With most buses the read must be done synchronously so this is most
  2247. * useful for devices with a cache which do not need to interact with
  2248. * the hardware to determine the current register value.
  2249. *
  2250. * Returns zero for success, a negative number on error.
  2251. */
  2252. int regmap_update_bits_base(struct regmap *map, unsigned int reg,
  2253. unsigned int mask, unsigned int val,
  2254. bool *change, bool async, bool force)
  2255. {
  2256. int ret;
  2257. map->lock(map->lock_arg);
  2258. map->async = async;
  2259. ret = _regmap_update_bits(map, reg, mask, val, change, force);
  2260. map->async = false;
  2261. map->unlock(map->lock_arg);
  2262. return ret;
  2263. }
  2264. EXPORT_SYMBOL_GPL(regmap_update_bits_base);
  2265. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  2266. {
  2267. struct regmap *map = async->map;
  2268. bool wake;
  2269. trace_regmap_async_io_complete(map);
  2270. spin_lock(&map->async_lock);
  2271. list_move(&async->list, &map->async_free);
  2272. wake = list_empty(&map->async_list);
  2273. if (ret != 0)
  2274. map->async_ret = ret;
  2275. spin_unlock(&map->async_lock);
  2276. if (wake)
  2277. wake_up(&map->async_waitq);
  2278. }
  2279. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  2280. static int regmap_async_is_done(struct regmap *map)
  2281. {
  2282. unsigned long flags;
  2283. int ret;
  2284. spin_lock_irqsave(&map->async_lock, flags);
  2285. ret = list_empty(&map->async_list);
  2286. spin_unlock_irqrestore(&map->async_lock, flags);
  2287. return ret;
  2288. }
  2289. /**
  2290. * regmap_async_complete: Ensure all asynchronous I/O has completed.
  2291. *
  2292. * @map: Map to operate on.
  2293. *
  2294. * Blocks until any pending asynchronous I/O has completed. Returns
  2295. * an error code for any failed I/O operations.
  2296. */
  2297. int regmap_async_complete(struct regmap *map)
  2298. {
  2299. unsigned long flags;
  2300. int ret;
  2301. /* Nothing to do with no async support */
  2302. if (!map->bus || !map->bus->async_write)
  2303. return 0;
  2304. trace_regmap_async_complete_start(map);
  2305. wait_event(map->async_waitq, regmap_async_is_done(map));
  2306. spin_lock_irqsave(&map->async_lock, flags);
  2307. ret = map->async_ret;
  2308. map->async_ret = 0;
  2309. spin_unlock_irqrestore(&map->async_lock, flags);
  2310. trace_regmap_async_complete_done(map);
  2311. return ret;
  2312. }
  2313. EXPORT_SYMBOL_GPL(regmap_async_complete);
  2314. /**
  2315. * regmap_register_patch: Register and apply register updates to be applied
  2316. * on device initialistion
  2317. *
  2318. * @map: Register map to apply updates to.
  2319. * @regs: Values to update.
  2320. * @num_regs: Number of entries in regs.
  2321. *
  2322. * Register a set of register updates to be applied to the device
  2323. * whenever the device registers are synchronised with the cache and
  2324. * apply them immediately. Typically this is used to apply
  2325. * corrections to be applied to the device defaults on startup, such
  2326. * as the updates some vendors provide to undocumented registers.
  2327. *
  2328. * The caller must ensure that this function cannot be called
  2329. * concurrently with either itself or regcache_sync().
  2330. */
  2331. int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
  2332. int num_regs)
  2333. {
  2334. struct reg_sequence *p;
  2335. int ret;
  2336. bool bypass;
  2337. if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
  2338. num_regs))
  2339. return 0;
  2340. p = krealloc(map->patch,
  2341. sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
  2342. GFP_KERNEL);
  2343. if (p) {
  2344. memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
  2345. map->patch = p;
  2346. map->patch_regs += num_regs;
  2347. } else {
  2348. return -ENOMEM;
  2349. }
  2350. map->lock(map->lock_arg);
  2351. bypass = map->cache_bypass;
  2352. map->cache_bypass = true;
  2353. map->async = true;
  2354. ret = _regmap_multi_reg_write(map, regs, num_regs);
  2355. map->async = false;
  2356. map->cache_bypass = bypass;
  2357. map->unlock(map->lock_arg);
  2358. regmap_async_complete(map);
  2359. return ret;
  2360. }
  2361. EXPORT_SYMBOL_GPL(regmap_register_patch);
  2362. /*
  2363. * regmap_get_val_bytes(): Report the size of a register value
  2364. *
  2365. * Report the size of a register value, mainly intended to for use by
  2366. * generic infrastructure built on top of regmap.
  2367. */
  2368. int regmap_get_val_bytes(struct regmap *map)
  2369. {
  2370. if (map->format.format_write)
  2371. return -EINVAL;
  2372. return map->format.val_bytes;
  2373. }
  2374. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  2375. /**
  2376. * regmap_get_max_register(): Report the max register value
  2377. *
  2378. * Report the max register value, mainly intended to for use by
  2379. * generic infrastructure built on top of regmap.
  2380. */
  2381. int regmap_get_max_register(struct regmap *map)
  2382. {
  2383. return map->max_register ? map->max_register : -EINVAL;
  2384. }
  2385. EXPORT_SYMBOL_GPL(regmap_get_max_register);
  2386. /**
  2387. * regmap_get_reg_stride(): Report the register address stride
  2388. *
  2389. * Report the register address stride, mainly intended to for use by
  2390. * generic infrastructure built on top of regmap.
  2391. */
  2392. int regmap_get_reg_stride(struct regmap *map)
  2393. {
  2394. return map->reg_stride;
  2395. }
  2396. EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
  2397. int regmap_parse_val(struct regmap *map, const void *buf,
  2398. unsigned int *val)
  2399. {
  2400. if (!map->format.parse_val)
  2401. return -EINVAL;
  2402. *val = map->format.parse_val(buf);
  2403. return 0;
  2404. }
  2405. EXPORT_SYMBOL_GPL(regmap_parse_val);
  2406. static int __init regmap_initcall(void)
  2407. {
  2408. regmap_debugfs_initcall();
  2409. return 0;
  2410. }
  2411. postcore_initcall(regmap_initcall);