regmap.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  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. rm_field->regmap = regmap;
  787. rm_field->reg = reg_field.reg;
  788. rm_field->shift = reg_field.lsb;
  789. rm_field->mask = GENMASK(reg_field.msb, 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, 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, reg, val_len / map->format.val_bytes);
  1123. /* If we're doing a single register write we can probably just
  1124. * send the work_buf directly, otherwise try to do a gather
  1125. * write.
  1126. */
  1127. if (val == work_val)
  1128. ret = map->bus->write(map->bus_context, map->work_buf,
  1129. map->format.reg_bytes +
  1130. map->format.pad_bytes +
  1131. val_len);
  1132. else if (map->bus->gather_write)
  1133. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  1134. map->format.reg_bytes +
  1135. map->format.pad_bytes,
  1136. val, val_len);
  1137. /* If that didn't work fall back on linearising by hand. */
  1138. if (ret == -ENOTSUPP) {
  1139. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  1140. buf = kzalloc(len, GFP_KERNEL);
  1141. if (!buf)
  1142. return -ENOMEM;
  1143. memcpy(buf, map->work_buf, map->format.reg_bytes);
  1144. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  1145. val, val_len);
  1146. ret = map->bus->write(map->bus_context, buf, len);
  1147. kfree(buf);
  1148. }
  1149. trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
  1150. return ret;
  1151. }
  1152. /**
  1153. * regmap_can_raw_write - Test if regmap_raw_write() is supported
  1154. *
  1155. * @map: Map to check.
  1156. */
  1157. bool regmap_can_raw_write(struct regmap *map)
  1158. {
  1159. return map->bus && map->format.format_val && map->format.format_reg;
  1160. }
  1161. EXPORT_SYMBOL_GPL(regmap_can_raw_write);
  1162. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  1163. unsigned int val)
  1164. {
  1165. int ret;
  1166. struct regmap_range_node *range;
  1167. struct regmap *map = context;
  1168. WARN_ON(!map->bus || !map->format.format_write);
  1169. range = _regmap_range_lookup(map, reg);
  1170. if (range) {
  1171. ret = _regmap_select_page(map, &reg, range, 1);
  1172. if (ret != 0)
  1173. return ret;
  1174. }
  1175. map->format.format_write(map, reg, val);
  1176. trace_regmap_hw_write_start(map, reg, 1);
  1177. ret = map->bus->write(map->bus_context, map->work_buf,
  1178. map->format.buf_size);
  1179. trace_regmap_hw_write_done(map, reg, 1);
  1180. return ret;
  1181. }
  1182. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  1183. unsigned int val)
  1184. {
  1185. struct regmap *map = context;
  1186. return map->bus->reg_write(map->bus_context, reg, val);
  1187. }
  1188. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  1189. unsigned int val)
  1190. {
  1191. struct regmap *map = context;
  1192. WARN_ON(!map->bus || !map->format.format_val);
  1193. map->format.format_val(map->work_buf + map->format.reg_bytes
  1194. + map->format.pad_bytes, val, 0);
  1195. return _regmap_raw_write(map, reg,
  1196. map->work_buf +
  1197. map->format.reg_bytes +
  1198. map->format.pad_bytes,
  1199. map->format.val_bytes);
  1200. }
  1201. static inline void *_regmap_map_get_context(struct regmap *map)
  1202. {
  1203. return (map->bus) ? map : map->bus_context;
  1204. }
  1205. int _regmap_write(struct regmap *map, unsigned int reg,
  1206. unsigned int val)
  1207. {
  1208. int ret;
  1209. void *context = _regmap_map_get_context(map);
  1210. if (!regmap_writeable(map, reg))
  1211. return -EIO;
  1212. if (!map->cache_bypass && !map->defer_caching) {
  1213. ret = regcache_write(map, reg, val);
  1214. if (ret != 0)
  1215. return ret;
  1216. if (map->cache_only) {
  1217. map->cache_dirty = true;
  1218. return 0;
  1219. }
  1220. }
  1221. #ifdef LOG_DEVICE
  1222. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1223. dev_info(map->dev, "%x <= %x\n", reg, val);
  1224. #endif
  1225. trace_regmap_reg_write(map, reg, val);
  1226. return map->reg_write(context, reg, val);
  1227. }
  1228. /**
  1229. * regmap_write(): Write a value to a single register
  1230. *
  1231. * @map: Register map to write to
  1232. * @reg: Register to write to
  1233. * @val: Value to be written
  1234. *
  1235. * A value of zero will be returned on success, a negative errno will
  1236. * be returned in error cases.
  1237. */
  1238. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  1239. {
  1240. int ret;
  1241. if (reg % map->reg_stride)
  1242. return -EINVAL;
  1243. map->lock(map->lock_arg);
  1244. ret = _regmap_write(map, reg, val);
  1245. map->unlock(map->lock_arg);
  1246. return ret;
  1247. }
  1248. EXPORT_SYMBOL_GPL(regmap_write);
  1249. /**
  1250. * regmap_write_async(): Write a value to a single register asynchronously
  1251. *
  1252. * @map: Register map to write to
  1253. * @reg: Register to write to
  1254. * @val: Value to be written
  1255. *
  1256. * A value of zero will be returned on success, a negative errno will
  1257. * be returned in error cases.
  1258. */
  1259. int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
  1260. {
  1261. int ret;
  1262. if (reg % map->reg_stride)
  1263. return -EINVAL;
  1264. map->lock(map->lock_arg);
  1265. map->async = true;
  1266. ret = _regmap_write(map, reg, val);
  1267. map->async = false;
  1268. map->unlock(map->lock_arg);
  1269. return ret;
  1270. }
  1271. EXPORT_SYMBOL_GPL(regmap_write_async);
  1272. /**
  1273. * regmap_raw_write(): Write raw values to one or more registers
  1274. *
  1275. * @map: Register map to write to
  1276. * @reg: Initial register to write to
  1277. * @val: Block of data to be written, laid out for direct transmission to the
  1278. * device
  1279. * @val_len: Length of data pointed to by val.
  1280. *
  1281. * This function is intended to be used for things like firmware
  1282. * download where a large block of data needs to be transferred to the
  1283. * device. No formatting will be done on the data provided.
  1284. *
  1285. * A value of zero will be returned on success, a negative errno will
  1286. * be returned in error cases.
  1287. */
  1288. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1289. const void *val, size_t val_len)
  1290. {
  1291. int ret;
  1292. if (!regmap_can_raw_write(map))
  1293. return -EINVAL;
  1294. if (val_len % map->format.val_bytes)
  1295. return -EINVAL;
  1296. map->lock(map->lock_arg);
  1297. ret = _regmap_raw_write(map, reg, val, val_len);
  1298. map->unlock(map->lock_arg);
  1299. return ret;
  1300. }
  1301. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1302. /**
  1303. * regmap_field_write(): Write a value to a single register field
  1304. *
  1305. * @field: Register field to write to
  1306. * @val: Value to be written
  1307. *
  1308. * A value of zero will be returned on success, a negative errno will
  1309. * be returned in error cases.
  1310. */
  1311. int regmap_field_write(struct regmap_field *field, unsigned int val)
  1312. {
  1313. return regmap_update_bits(field->regmap, field->reg,
  1314. field->mask, val << field->shift);
  1315. }
  1316. EXPORT_SYMBOL_GPL(regmap_field_write);
  1317. /**
  1318. * regmap_field_update_bits(): Perform a read/modify/write cycle
  1319. * on the register field
  1320. *
  1321. * @field: Register field to write to
  1322. * @mask: Bitmask to change
  1323. * @val: Value to be written
  1324. *
  1325. * A value of zero will be returned on success, a negative errno will
  1326. * be returned in error cases.
  1327. */
  1328. int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
  1329. {
  1330. mask = (mask << field->shift) & field->mask;
  1331. return regmap_update_bits(field->regmap, field->reg,
  1332. mask, val << field->shift);
  1333. }
  1334. EXPORT_SYMBOL_GPL(regmap_field_update_bits);
  1335. /**
  1336. * regmap_fields_write(): Write a value to a single register field with port ID
  1337. *
  1338. * @field: Register field to write to
  1339. * @id: port ID
  1340. * @val: Value to be written
  1341. *
  1342. * A value of zero will be returned on success, a negative errno will
  1343. * be returned in error cases.
  1344. */
  1345. int regmap_fields_write(struct regmap_field *field, unsigned int id,
  1346. unsigned int val)
  1347. {
  1348. if (id >= field->id_size)
  1349. return -EINVAL;
  1350. return regmap_update_bits(field->regmap,
  1351. field->reg + (field->id_offset * id),
  1352. field->mask, val << field->shift);
  1353. }
  1354. EXPORT_SYMBOL_GPL(regmap_fields_write);
  1355. /**
  1356. * regmap_fields_update_bits(): Perform a read/modify/write cycle
  1357. * on the register field
  1358. *
  1359. * @field: Register field to write to
  1360. * @id: port ID
  1361. * @mask: Bitmask to change
  1362. * @val: Value to be written
  1363. *
  1364. * A value of zero will be returned on success, a negative errno will
  1365. * be returned in error cases.
  1366. */
  1367. int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
  1368. unsigned int mask, unsigned int val)
  1369. {
  1370. if (id >= field->id_size)
  1371. return -EINVAL;
  1372. mask = (mask << field->shift) & field->mask;
  1373. return regmap_update_bits(field->regmap,
  1374. field->reg + (field->id_offset * id),
  1375. mask, val << field->shift);
  1376. }
  1377. EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
  1378. /*
  1379. * regmap_bulk_write(): Write multiple registers to the device
  1380. *
  1381. * @map: Register map to write to
  1382. * @reg: First register to be write from
  1383. * @val: Block of data to be written, in native register size for device
  1384. * @val_count: Number of registers to write
  1385. *
  1386. * This function is intended to be used for writing a large block of
  1387. * data to the device either in single transfer or multiple transfer.
  1388. *
  1389. * A value of zero will be returned on success, a negative errno will
  1390. * be returned in error cases.
  1391. */
  1392. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1393. size_t val_count)
  1394. {
  1395. int ret = 0, i;
  1396. size_t val_bytes = map->format.val_bytes;
  1397. if (map->bus && !map->format.parse_inplace)
  1398. return -EINVAL;
  1399. if (reg % map->reg_stride)
  1400. return -EINVAL;
  1401. /*
  1402. * Some devices don't support bulk write, for
  1403. * them we have a series of single write operations.
  1404. */
  1405. if (!map->bus || map->use_single_rw) {
  1406. map->lock(map->lock_arg);
  1407. for (i = 0; i < val_count; i++) {
  1408. unsigned int ival;
  1409. switch (val_bytes) {
  1410. case 1:
  1411. ival = *(u8 *)(val + (i * val_bytes));
  1412. break;
  1413. case 2:
  1414. ival = *(u16 *)(val + (i * val_bytes));
  1415. break;
  1416. case 4:
  1417. ival = *(u32 *)(val + (i * val_bytes));
  1418. break;
  1419. #ifdef CONFIG_64BIT
  1420. case 8:
  1421. ival = *(u64 *)(val + (i * val_bytes));
  1422. break;
  1423. #endif
  1424. default:
  1425. ret = -EINVAL;
  1426. goto out;
  1427. }
  1428. ret = _regmap_write(map, reg + (i * map->reg_stride),
  1429. ival);
  1430. if (ret != 0)
  1431. goto out;
  1432. }
  1433. out:
  1434. map->unlock(map->lock_arg);
  1435. } else {
  1436. void *wval;
  1437. if (!val_count)
  1438. return -EINVAL;
  1439. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  1440. if (!wval) {
  1441. dev_err(map->dev, "Error in memory allocation\n");
  1442. return -ENOMEM;
  1443. }
  1444. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1445. map->format.parse_inplace(wval + i);
  1446. map->lock(map->lock_arg);
  1447. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
  1448. map->unlock(map->lock_arg);
  1449. kfree(wval);
  1450. }
  1451. return ret;
  1452. }
  1453. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1454. /*
  1455. * _regmap_raw_multi_reg_write()
  1456. *
  1457. * the (register,newvalue) pairs in regs have not been formatted, but
  1458. * they are all in the same page and have been changed to being page
  1459. * relative. The page register has been written if that was neccessary.
  1460. */
  1461. static int _regmap_raw_multi_reg_write(struct regmap *map,
  1462. const struct reg_default *regs,
  1463. size_t num_regs)
  1464. {
  1465. int ret;
  1466. void *buf;
  1467. int i;
  1468. u8 *u8;
  1469. size_t val_bytes = map->format.val_bytes;
  1470. size_t reg_bytes = map->format.reg_bytes;
  1471. size_t pad_bytes = map->format.pad_bytes;
  1472. size_t pair_size = reg_bytes + pad_bytes + val_bytes;
  1473. size_t len = pair_size * num_regs;
  1474. if (!len)
  1475. return -EINVAL;
  1476. buf = kzalloc(len, GFP_KERNEL);
  1477. if (!buf)
  1478. return -ENOMEM;
  1479. /* We have to linearise by hand. */
  1480. u8 = buf;
  1481. for (i = 0; i < num_regs; i++) {
  1482. int reg = regs[i].reg;
  1483. int val = regs[i].def;
  1484. trace_regmap_hw_write_start(map, reg, 1);
  1485. map->format.format_reg(u8, reg, map->reg_shift);
  1486. u8 += reg_bytes + pad_bytes;
  1487. map->format.format_val(u8, val, 0);
  1488. u8 += val_bytes;
  1489. }
  1490. u8 = buf;
  1491. *u8 |= map->write_flag_mask;
  1492. ret = map->bus->write(map->bus_context, buf, len);
  1493. kfree(buf);
  1494. for (i = 0; i < num_regs; i++) {
  1495. int reg = regs[i].reg;
  1496. trace_regmap_hw_write_done(map, reg, 1);
  1497. }
  1498. return ret;
  1499. }
  1500. static unsigned int _regmap_register_page(struct regmap *map,
  1501. unsigned int reg,
  1502. struct regmap_range_node *range)
  1503. {
  1504. unsigned int win_page = (reg - range->range_min) / range->window_len;
  1505. return win_page;
  1506. }
  1507. static int _regmap_range_multi_paged_reg_write(struct regmap *map,
  1508. struct reg_default *regs,
  1509. size_t num_regs)
  1510. {
  1511. int ret;
  1512. int i, n;
  1513. struct reg_default *base;
  1514. unsigned int this_page = 0;
  1515. /*
  1516. * the set of registers are not neccessarily in order, but
  1517. * since the order of write must be preserved this algorithm
  1518. * chops the set each time the page changes
  1519. */
  1520. base = regs;
  1521. for (i = 0, n = 0; i < num_regs; i++, n++) {
  1522. unsigned int reg = regs[i].reg;
  1523. struct regmap_range_node *range;
  1524. range = _regmap_range_lookup(map, reg);
  1525. if (range) {
  1526. unsigned int win_page = _regmap_register_page(map, reg,
  1527. range);
  1528. if (i == 0)
  1529. this_page = win_page;
  1530. if (win_page != this_page) {
  1531. this_page = win_page;
  1532. ret = _regmap_raw_multi_reg_write(map, base, n);
  1533. if (ret != 0)
  1534. return ret;
  1535. base += n;
  1536. n = 0;
  1537. }
  1538. ret = _regmap_select_page(map, &base[n].reg, range, 1);
  1539. if (ret != 0)
  1540. return ret;
  1541. }
  1542. }
  1543. if (n > 0)
  1544. return _regmap_raw_multi_reg_write(map, base, n);
  1545. return 0;
  1546. }
  1547. static int _regmap_multi_reg_write(struct regmap *map,
  1548. const struct reg_default *regs,
  1549. size_t num_regs)
  1550. {
  1551. int i;
  1552. int ret;
  1553. if (!map->can_multi_write) {
  1554. for (i = 0; i < num_regs; i++) {
  1555. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1556. if (ret != 0)
  1557. return ret;
  1558. }
  1559. return 0;
  1560. }
  1561. if (!map->format.parse_inplace)
  1562. return -EINVAL;
  1563. if (map->writeable_reg)
  1564. for (i = 0; i < num_regs; i++) {
  1565. int reg = regs[i].reg;
  1566. if (!map->writeable_reg(map->dev, reg))
  1567. return -EINVAL;
  1568. if (reg % map->reg_stride)
  1569. return -EINVAL;
  1570. }
  1571. if (!map->cache_bypass) {
  1572. for (i = 0; i < num_regs; i++) {
  1573. unsigned int val = regs[i].def;
  1574. unsigned int reg = regs[i].reg;
  1575. ret = regcache_write(map, reg, val);
  1576. if (ret) {
  1577. dev_err(map->dev,
  1578. "Error in caching of register: %x ret: %d\n",
  1579. reg, ret);
  1580. return ret;
  1581. }
  1582. }
  1583. if (map->cache_only) {
  1584. map->cache_dirty = true;
  1585. return 0;
  1586. }
  1587. }
  1588. WARN_ON(!map->bus);
  1589. for (i = 0; i < num_regs; i++) {
  1590. unsigned int reg = regs[i].reg;
  1591. struct regmap_range_node *range;
  1592. range = _regmap_range_lookup(map, reg);
  1593. if (range) {
  1594. size_t len = sizeof(struct reg_default)*num_regs;
  1595. struct reg_default *base = kmemdup(regs, len,
  1596. GFP_KERNEL);
  1597. if (!base)
  1598. return -ENOMEM;
  1599. ret = _regmap_range_multi_paged_reg_write(map, base,
  1600. num_regs);
  1601. kfree(base);
  1602. return ret;
  1603. }
  1604. }
  1605. return _regmap_raw_multi_reg_write(map, regs, num_regs);
  1606. }
  1607. /*
  1608. * regmap_multi_reg_write(): Write multiple registers to the device
  1609. *
  1610. * where the set of register,value pairs are supplied in any order,
  1611. * possibly not all in a single range.
  1612. *
  1613. * @map: Register map to write to
  1614. * @regs: Array of structures containing register,value to be written
  1615. * @num_regs: Number of registers to write
  1616. *
  1617. * The 'normal' block write mode will send ultimately send data on the
  1618. * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
  1619. * addressed. However, this alternative block multi write mode will send
  1620. * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
  1621. * must of course support the mode.
  1622. *
  1623. * A value of zero will be returned on success, a negative errno will be
  1624. * returned in error cases.
  1625. */
  1626. int regmap_multi_reg_write(struct regmap *map, const struct reg_default *regs,
  1627. int num_regs)
  1628. {
  1629. int ret;
  1630. map->lock(map->lock_arg);
  1631. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1632. map->unlock(map->lock_arg);
  1633. return ret;
  1634. }
  1635. EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
  1636. /*
  1637. * regmap_multi_reg_write_bypassed(): Write multiple registers to the
  1638. * device but not the cache
  1639. *
  1640. * where the set of register are supplied in any order
  1641. *
  1642. * @map: Register map to write to
  1643. * @regs: Array of structures containing register,value to be written
  1644. * @num_regs: Number of registers to write
  1645. *
  1646. * This function is intended to be used for writing a large block of data
  1647. * atomically to the device in single transfer for those I2C client devices
  1648. * that implement this alternative block write mode.
  1649. *
  1650. * A value of zero will be returned on success, a negative errno will
  1651. * be returned in error cases.
  1652. */
  1653. int regmap_multi_reg_write_bypassed(struct regmap *map,
  1654. const struct reg_default *regs,
  1655. int num_regs)
  1656. {
  1657. int ret;
  1658. bool bypass;
  1659. map->lock(map->lock_arg);
  1660. bypass = map->cache_bypass;
  1661. map->cache_bypass = true;
  1662. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1663. map->cache_bypass = bypass;
  1664. map->unlock(map->lock_arg);
  1665. return ret;
  1666. }
  1667. EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
  1668. /**
  1669. * regmap_raw_write_async(): Write raw values to one or more registers
  1670. * asynchronously
  1671. *
  1672. * @map: Register map to write to
  1673. * @reg: Initial register to write to
  1674. * @val: Block of data to be written, laid out for direct transmission to the
  1675. * device. Must be valid until regmap_async_complete() is called.
  1676. * @val_len: Length of data pointed to by val.
  1677. *
  1678. * This function is intended to be used for things like firmware
  1679. * download where a large block of data needs to be transferred to the
  1680. * device. No formatting will be done on the data provided.
  1681. *
  1682. * If supported by the underlying bus the write will be scheduled
  1683. * asynchronously, helping maximise I/O speed on higher speed buses
  1684. * like SPI. regmap_async_complete() can be called to ensure that all
  1685. * asynchrnous writes have been completed.
  1686. *
  1687. * A value of zero will be returned on success, a negative errno will
  1688. * be returned in error cases.
  1689. */
  1690. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  1691. const void *val, size_t val_len)
  1692. {
  1693. int ret;
  1694. if (val_len % map->format.val_bytes)
  1695. return -EINVAL;
  1696. if (reg % map->reg_stride)
  1697. return -EINVAL;
  1698. map->lock(map->lock_arg);
  1699. map->async = true;
  1700. ret = _regmap_raw_write(map, reg, val, val_len);
  1701. map->async = false;
  1702. map->unlock(map->lock_arg);
  1703. return ret;
  1704. }
  1705. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  1706. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1707. unsigned int val_len)
  1708. {
  1709. struct regmap_range_node *range;
  1710. u8 *u8 = map->work_buf;
  1711. int ret;
  1712. WARN_ON(!map->bus);
  1713. range = _regmap_range_lookup(map, reg);
  1714. if (range) {
  1715. ret = _regmap_select_page(map, &reg, range,
  1716. val_len / map->format.val_bytes);
  1717. if (ret != 0)
  1718. return ret;
  1719. }
  1720. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1721. /*
  1722. * Some buses or devices flag reads by setting the high bits in the
  1723. * register addresss; since it's always the high bits for all
  1724. * current formats we can do this here rather than in
  1725. * formatting. This may break if we get interesting formats.
  1726. */
  1727. u8[0] |= map->read_flag_mask;
  1728. trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
  1729. ret = map->bus->read(map->bus_context, map->work_buf,
  1730. map->format.reg_bytes + map->format.pad_bytes,
  1731. val, val_len);
  1732. trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
  1733. return ret;
  1734. }
  1735. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  1736. unsigned int *val)
  1737. {
  1738. struct regmap *map = context;
  1739. return map->bus->reg_read(map->bus_context, reg, val);
  1740. }
  1741. static int _regmap_bus_read(void *context, unsigned int reg,
  1742. unsigned int *val)
  1743. {
  1744. int ret;
  1745. struct regmap *map = context;
  1746. if (!map->format.parse_val)
  1747. return -EINVAL;
  1748. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  1749. if (ret == 0)
  1750. *val = map->format.parse_val(map->work_buf);
  1751. return ret;
  1752. }
  1753. static int _regmap_read(struct regmap *map, unsigned int reg,
  1754. unsigned int *val)
  1755. {
  1756. int ret;
  1757. void *context = _regmap_map_get_context(map);
  1758. WARN_ON(!map->reg_read);
  1759. if (!map->cache_bypass) {
  1760. ret = regcache_read(map, reg, val);
  1761. if (ret == 0)
  1762. return 0;
  1763. }
  1764. if (map->cache_only)
  1765. return -EBUSY;
  1766. if (!regmap_readable(map, reg))
  1767. return -EIO;
  1768. ret = map->reg_read(context, reg, val);
  1769. if (ret == 0) {
  1770. #ifdef LOG_DEVICE
  1771. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1772. dev_info(map->dev, "%x => %x\n", reg, *val);
  1773. #endif
  1774. trace_regmap_reg_read(map, reg, *val);
  1775. if (!map->cache_bypass)
  1776. regcache_write(map, reg, *val);
  1777. }
  1778. return ret;
  1779. }
  1780. /**
  1781. * regmap_read(): Read a value from a single register
  1782. *
  1783. * @map: Register map to read from
  1784. * @reg: Register to be read from
  1785. * @val: Pointer to store read value
  1786. *
  1787. * A value of zero will be returned on success, a negative errno will
  1788. * be returned in error cases.
  1789. */
  1790. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  1791. {
  1792. int ret;
  1793. if (reg % map->reg_stride)
  1794. return -EINVAL;
  1795. map->lock(map->lock_arg);
  1796. ret = _regmap_read(map, reg, val);
  1797. map->unlock(map->lock_arg);
  1798. return ret;
  1799. }
  1800. EXPORT_SYMBOL_GPL(regmap_read);
  1801. /**
  1802. * regmap_raw_read(): Read raw data from the device
  1803. *
  1804. * @map: Register map to read from
  1805. * @reg: First register to be read from
  1806. * @val: Pointer to store read value
  1807. * @val_len: Size of data to read
  1808. *
  1809. * A value of zero will be returned on success, a negative errno will
  1810. * be returned in error cases.
  1811. */
  1812. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1813. size_t val_len)
  1814. {
  1815. size_t val_bytes = map->format.val_bytes;
  1816. size_t val_count = val_len / val_bytes;
  1817. unsigned int v;
  1818. int ret, i;
  1819. if (!map->bus)
  1820. return -EINVAL;
  1821. if (val_len % map->format.val_bytes)
  1822. return -EINVAL;
  1823. if (reg % map->reg_stride)
  1824. return -EINVAL;
  1825. map->lock(map->lock_arg);
  1826. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  1827. map->cache_type == REGCACHE_NONE) {
  1828. /* Physical block read if there's no cache involved */
  1829. ret = _regmap_raw_read(map, reg, val, val_len);
  1830. } else {
  1831. /* Otherwise go word by word for the cache; should be low
  1832. * cost as we expect to hit the cache.
  1833. */
  1834. for (i = 0; i < val_count; i++) {
  1835. ret = _regmap_read(map, reg + (i * map->reg_stride),
  1836. &v);
  1837. if (ret != 0)
  1838. goto out;
  1839. map->format.format_val(val + (i * val_bytes), v, 0);
  1840. }
  1841. }
  1842. out:
  1843. map->unlock(map->lock_arg);
  1844. return ret;
  1845. }
  1846. EXPORT_SYMBOL_GPL(regmap_raw_read);
  1847. /**
  1848. * regmap_field_read(): Read a value to a single register field
  1849. *
  1850. * @field: Register field to read from
  1851. * @val: Pointer to store read value
  1852. *
  1853. * A value of zero will be returned on success, a negative errno will
  1854. * be returned in error cases.
  1855. */
  1856. int regmap_field_read(struct regmap_field *field, unsigned int *val)
  1857. {
  1858. int ret;
  1859. unsigned int reg_val;
  1860. ret = regmap_read(field->regmap, field->reg, &reg_val);
  1861. if (ret != 0)
  1862. return ret;
  1863. reg_val &= field->mask;
  1864. reg_val >>= field->shift;
  1865. *val = reg_val;
  1866. return ret;
  1867. }
  1868. EXPORT_SYMBOL_GPL(regmap_field_read);
  1869. /**
  1870. * regmap_fields_read(): Read a value to a single register field with port ID
  1871. *
  1872. * @field: Register field to read from
  1873. * @id: port ID
  1874. * @val: Pointer to store read value
  1875. *
  1876. * A value of zero will be returned on success, a negative errno will
  1877. * be returned in error cases.
  1878. */
  1879. int regmap_fields_read(struct regmap_field *field, unsigned int id,
  1880. unsigned int *val)
  1881. {
  1882. int ret;
  1883. unsigned int reg_val;
  1884. if (id >= field->id_size)
  1885. return -EINVAL;
  1886. ret = regmap_read(field->regmap,
  1887. field->reg + (field->id_offset * id),
  1888. &reg_val);
  1889. if (ret != 0)
  1890. return ret;
  1891. reg_val &= field->mask;
  1892. reg_val >>= field->shift;
  1893. *val = reg_val;
  1894. return ret;
  1895. }
  1896. EXPORT_SYMBOL_GPL(regmap_fields_read);
  1897. /**
  1898. * regmap_bulk_read(): Read multiple registers from the device
  1899. *
  1900. * @map: Register map to read from
  1901. * @reg: First register to be read from
  1902. * @val: Pointer to store read value, in native register size for device
  1903. * @val_count: Number of registers to read
  1904. *
  1905. * A value of zero will be returned on success, a negative errno will
  1906. * be returned in error cases.
  1907. */
  1908. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  1909. size_t val_count)
  1910. {
  1911. int ret, i;
  1912. size_t val_bytes = map->format.val_bytes;
  1913. bool vol = regmap_volatile_range(map, reg, val_count);
  1914. if (reg % map->reg_stride)
  1915. return -EINVAL;
  1916. if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
  1917. /*
  1918. * Some devices does not support bulk read, for
  1919. * them we have a series of single read operations.
  1920. */
  1921. if (map->use_single_rw) {
  1922. for (i = 0; i < val_count; i++) {
  1923. ret = regmap_raw_read(map,
  1924. reg + (i * map->reg_stride),
  1925. val + (i * val_bytes),
  1926. val_bytes);
  1927. if (ret != 0)
  1928. return ret;
  1929. }
  1930. } else {
  1931. ret = regmap_raw_read(map, reg, val,
  1932. val_bytes * val_count);
  1933. if (ret != 0)
  1934. return ret;
  1935. }
  1936. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1937. map->format.parse_inplace(val + i);
  1938. } else {
  1939. for (i = 0; i < val_count; i++) {
  1940. unsigned int ival;
  1941. ret = regmap_read(map, reg + (i * map->reg_stride),
  1942. &ival);
  1943. if (ret != 0)
  1944. return ret;
  1945. map->format.format_val(val + (i * val_bytes), ival, 0);
  1946. }
  1947. }
  1948. return 0;
  1949. }
  1950. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  1951. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  1952. unsigned int mask, unsigned int val,
  1953. bool *change)
  1954. {
  1955. int ret;
  1956. unsigned int tmp, orig;
  1957. ret = _regmap_read(map, reg, &orig);
  1958. if (ret != 0)
  1959. return ret;
  1960. tmp = orig & ~mask;
  1961. tmp |= val & mask;
  1962. if (tmp != orig) {
  1963. ret = _regmap_write(map, reg, tmp);
  1964. if (change)
  1965. *change = true;
  1966. } else {
  1967. if (change)
  1968. *change = false;
  1969. }
  1970. return ret;
  1971. }
  1972. /**
  1973. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  1974. *
  1975. * @map: Register map to update
  1976. * @reg: Register to update
  1977. * @mask: Bitmask to change
  1978. * @val: New value for bitmask
  1979. *
  1980. * Returns zero for success, a negative number on error.
  1981. */
  1982. int regmap_update_bits(struct regmap *map, unsigned int reg,
  1983. unsigned int mask, unsigned int val)
  1984. {
  1985. int ret;
  1986. map->lock(map->lock_arg);
  1987. ret = _regmap_update_bits(map, reg, mask, val, NULL);
  1988. map->unlock(map->lock_arg);
  1989. return ret;
  1990. }
  1991. EXPORT_SYMBOL_GPL(regmap_update_bits);
  1992. /**
  1993. * regmap_update_bits_async: Perform a read/modify/write cycle on the register
  1994. * map asynchronously
  1995. *
  1996. * @map: Register map to update
  1997. * @reg: Register to update
  1998. * @mask: Bitmask to change
  1999. * @val: New value for bitmask
  2000. *
  2001. * With most buses the read must be done synchronously so this is most
  2002. * useful for devices with a cache which do not need to interact with
  2003. * the hardware to determine the current register value.
  2004. *
  2005. * Returns zero for success, a negative number on error.
  2006. */
  2007. int regmap_update_bits_async(struct regmap *map, unsigned int reg,
  2008. unsigned int mask, unsigned int val)
  2009. {
  2010. int ret;
  2011. map->lock(map->lock_arg);
  2012. map->async = true;
  2013. ret = _regmap_update_bits(map, reg, mask, val, NULL);
  2014. map->async = false;
  2015. map->unlock(map->lock_arg);
  2016. return ret;
  2017. }
  2018. EXPORT_SYMBOL_GPL(regmap_update_bits_async);
  2019. /**
  2020. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  2021. * register map and report if updated
  2022. *
  2023. * @map: Register map to update
  2024. * @reg: Register to update
  2025. * @mask: Bitmask to change
  2026. * @val: New value for bitmask
  2027. * @change: Boolean indicating if a write was done
  2028. *
  2029. * Returns zero for success, a negative number on error.
  2030. */
  2031. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  2032. unsigned int mask, unsigned int val,
  2033. bool *change)
  2034. {
  2035. int ret;
  2036. map->lock(map->lock_arg);
  2037. ret = _regmap_update_bits(map, reg, mask, val, change);
  2038. map->unlock(map->lock_arg);
  2039. return ret;
  2040. }
  2041. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  2042. /**
  2043. * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
  2044. * register map asynchronously and report if
  2045. * updated
  2046. *
  2047. * @map: Register map to update
  2048. * @reg: Register to update
  2049. * @mask: Bitmask to change
  2050. * @val: New value for bitmask
  2051. * @change: Boolean indicating if a write was done
  2052. *
  2053. * With most buses the read must be done synchronously so this is most
  2054. * useful for devices with a cache which do not need to interact with
  2055. * the hardware to determine the current register value.
  2056. *
  2057. * Returns zero for success, a negative number on error.
  2058. */
  2059. int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
  2060. unsigned int mask, unsigned int val,
  2061. bool *change)
  2062. {
  2063. int ret;
  2064. map->lock(map->lock_arg);
  2065. map->async = true;
  2066. ret = _regmap_update_bits(map, reg, mask, val, change);
  2067. map->async = false;
  2068. map->unlock(map->lock_arg);
  2069. return ret;
  2070. }
  2071. EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
  2072. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  2073. {
  2074. struct regmap *map = async->map;
  2075. bool wake;
  2076. trace_regmap_async_io_complete(map);
  2077. spin_lock(&map->async_lock);
  2078. list_move(&async->list, &map->async_free);
  2079. wake = list_empty(&map->async_list);
  2080. if (ret != 0)
  2081. map->async_ret = ret;
  2082. spin_unlock(&map->async_lock);
  2083. if (wake)
  2084. wake_up(&map->async_waitq);
  2085. }
  2086. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  2087. static int regmap_async_is_done(struct regmap *map)
  2088. {
  2089. unsigned long flags;
  2090. int ret;
  2091. spin_lock_irqsave(&map->async_lock, flags);
  2092. ret = list_empty(&map->async_list);
  2093. spin_unlock_irqrestore(&map->async_lock, flags);
  2094. return ret;
  2095. }
  2096. /**
  2097. * regmap_async_complete: Ensure all asynchronous I/O has completed.
  2098. *
  2099. * @map: Map to operate on.
  2100. *
  2101. * Blocks until any pending asynchronous I/O has completed. Returns
  2102. * an error code for any failed I/O operations.
  2103. */
  2104. int regmap_async_complete(struct regmap *map)
  2105. {
  2106. unsigned long flags;
  2107. int ret;
  2108. /* Nothing to do with no async support */
  2109. if (!map->bus || !map->bus->async_write)
  2110. return 0;
  2111. trace_regmap_async_complete_start(map);
  2112. wait_event(map->async_waitq, regmap_async_is_done(map));
  2113. spin_lock_irqsave(&map->async_lock, flags);
  2114. ret = map->async_ret;
  2115. map->async_ret = 0;
  2116. spin_unlock_irqrestore(&map->async_lock, flags);
  2117. trace_regmap_async_complete_done(map);
  2118. return ret;
  2119. }
  2120. EXPORT_SYMBOL_GPL(regmap_async_complete);
  2121. /**
  2122. * regmap_register_patch: Register and apply register updates to be applied
  2123. * on device initialistion
  2124. *
  2125. * @map: Register map to apply updates to.
  2126. * @regs: Values to update.
  2127. * @num_regs: Number of entries in regs.
  2128. *
  2129. * Register a set of register updates to be applied to the device
  2130. * whenever the device registers are synchronised with the cache and
  2131. * apply them immediately. Typically this is used to apply
  2132. * corrections to be applied to the device defaults on startup, such
  2133. * as the updates some vendors provide to undocumented registers.
  2134. *
  2135. * The caller must ensure that this function cannot be called
  2136. * concurrently with either itself or regcache_sync().
  2137. */
  2138. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  2139. int num_regs)
  2140. {
  2141. struct reg_default *p;
  2142. int ret;
  2143. bool bypass;
  2144. if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
  2145. num_regs))
  2146. return 0;
  2147. p = krealloc(map->patch,
  2148. sizeof(struct reg_default) * (map->patch_regs + num_regs),
  2149. GFP_KERNEL);
  2150. if (p) {
  2151. memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
  2152. map->patch = p;
  2153. map->patch_regs += num_regs;
  2154. } else {
  2155. return -ENOMEM;
  2156. }
  2157. map->lock(map->lock_arg);
  2158. bypass = map->cache_bypass;
  2159. map->cache_bypass = true;
  2160. map->async = true;
  2161. ret = _regmap_multi_reg_write(map, regs, num_regs);
  2162. map->async = false;
  2163. map->cache_bypass = bypass;
  2164. map->unlock(map->lock_arg);
  2165. regmap_async_complete(map);
  2166. return ret;
  2167. }
  2168. EXPORT_SYMBOL_GPL(regmap_register_patch);
  2169. /*
  2170. * regmap_get_val_bytes(): Report the size of a register value
  2171. *
  2172. * Report the size of a register value, mainly intended to for use by
  2173. * generic infrastructure built on top of regmap.
  2174. */
  2175. int regmap_get_val_bytes(struct regmap *map)
  2176. {
  2177. if (map->format.format_write)
  2178. return -EINVAL;
  2179. return map->format.val_bytes;
  2180. }
  2181. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  2182. /**
  2183. * regmap_get_max_register(): Report the max register value
  2184. *
  2185. * Report the max register value, mainly intended to for use by
  2186. * generic infrastructure built on top of regmap.
  2187. */
  2188. int regmap_get_max_register(struct regmap *map)
  2189. {
  2190. return map->max_register ? map->max_register : -EINVAL;
  2191. }
  2192. EXPORT_SYMBOL_GPL(regmap_get_max_register);
  2193. /**
  2194. * regmap_get_reg_stride(): Report the register address stride
  2195. *
  2196. * Report the register address stride, mainly intended to for use by
  2197. * generic infrastructure built on top of regmap.
  2198. */
  2199. int regmap_get_reg_stride(struct regmap *map)
  2200. {
  2201. return map->reg_stride;
  2202. }
  2203. EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
  2204. int regmap_parse_val(struct regmap *map, const void *buf,
  2205. unsigned int *val)
  2206. {
  2207. if (!map->format.parse_val)
  2208. return -EINVAL;
  2209. *val = map->format.parse_val(buf);
  2210. return 0;
  2211. }
  2212. EXPORT_SYMBOL_GPL(regmap_parse_val);
  2213. static int __init regmap_initcall(void)
  2214. {
  2215. regmap_debugfs_initcall();
  2216. return 0;
  2217. }
  2218. postcore_initcall(regmap_initcall);