regmap.c 50 KB

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