regmap.c 53 KB

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