regmap.c 42 KB

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