regmap.c 62 KB

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