regmap.c 41 KB

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