regmap.c 47 KB

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