regmap.c 63 KB

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