regmap.c 63 KB

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