regmap.c 70 KB

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