regmap.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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. /* Allow data window inside its own virtual range */
  575. if (j == i)
  576. continue;
  577. if (range_cfg->range_min <= sel_reg &&
  578. sel_reg <= range_cfg->range_max) {
  579. dev_err(map->dev,
  580. "Range %d: selector for %d in window\n",
  581. i, j);
  582. goto err_range;
  583. }
  584. if (!(win_max < range_cfg->range_min ||
  585. win_min > range_cfg->range_max)) {
  586. dev_err(map->dev,
  587. "Range %d: window for %d in window\n",
  588. i, j);
  589. goto err_range;
  590. }
  591. }
  592. new = kzalloc(sizeof(*new), GFP_KERNEL);
  593. if (new == NULL) {
  594. ret = -ENOMEM;
  595. goto err_range;
  596. }
  597. new->map = map;
  598. new->name = range_cfg->name;
  599. new->range_min = range_cfg->range_min;
  600. new->range_max = range_cfg->range_max;
  601. new->selector_reg = range_cfg->selector_reg;
  602. new->selector_mask = range_cfg->selector_mask;
  603. new->selector_shift = range_cfg->selector_shift;
  604. new->window_start = range_cfg->window_start;
  605. new->window_len = range_cfg->window_len;
  606. if (_regmap_range_add(map, new) == false) {
  607. dev_err(map->dev, "Failed to add range %d\n", i);
  608. kfree(new);
  609. goto err_range;
  610. }
  611. if (map->selector_work_buf == NULL) {
  612. map->selector_work_buf =
  613. kzalloc(map->format.buf_size, GFP_KERNEL);
  614. if (map->selector_work_buf == NULL) {
  615. ret = -ENOMEM;
  616. goto err_range;
  617. }
  618. }
  619. }
  620. regmap_debugfs_init(map, config->name);
  621. ret = regcache_init(map, config);
  622. if (ret != 0)
  623. goto err_range;
  624. /* Add a devres resource for dev_get_regmap() */
  625. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  626. if (!m) {
  627. ret = -ENOMEM;
  628. goto err_debugfs;
  629. }
  630. *m = map;
  631. devres_add(dev, m);
  632. return map;
  633. err_debugfs:
  634. regmap_debugfs_exit(map);
  635. regcache_exit(map);
  636. err_range:
  637. regmap_range_exit(map);
  638. kfree(map->work_buf);
  639. err_map:
  640. kfree(map);
  641. err:
  642. return ERR_PTR(ret);
  643. }
  644. EXPORT_SYMBOL_GPL(regmap_init);
  645. static void devm_regmap_release(struct device *dev, void *res)
  646. {
  647. regmap_exit(*(struct regmap **)res);
  648. }
  649. /**
  650. * devm_regmap_init(): Initialise managed register map
  651. *
  652. * @dev: Device that will be interacted with
  653. * @bus: Bus-specific callbacks to use with device
  654. * @bus_context: Data passed to bus-specific callbacks
  655. * @config: Configuration for register map
  656. *
  657. * The return value will be an ERR_PTR() on error or a valid pointer
  658. * to a struct regmap. This function should generally not be called
  659. * directly, it should be called by bus-specific init functions. The
  660. * map will be automatically freed by the device management code.
  661. */
  662. struct regmap *devm_regmap_init(struct device *dev,
  663. const struct regmap_bus *bus,
  664. void *bus_context,
  665. const struct regmap_config *config)
  666. {
  667. struct regmap **ptr, *regmap;
  668. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  669. if (!ptr)
  670. return ERR_PTR(-ENOMEM);
  671. regmap = regmap_init(dev, bus, bus_context, config);
  672. if (!IS_ERR(regmap)) {
  673. *ptr = regmap;
  674. devres_add(dev, ptr);
  675. } else {
  676. devres_free(ptr);
  677. }
  678. return regmap;
  679. }
  680. EXPORT_SYMBOL_GPL(devm_regmap_init);
  681. static void regmap_field_init(struct regmap_field *rm_field,
  682. struct regmap *regmap, struct reg_field reg_field)
  683. {
  684. int field_bits = reg_field.msb - reg_field.lsb + 1;
  685. rm_field->regmap = regmap;
  686. rm_field->reg = reg_field.reg;
  687. rm_field->shift = reg_field.lsb;
  688. rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
  689. }
  690. /**
  691. * devm_regmap_field_alloc(): Allocate and initialise a register field
  692. * in a register map.
  693. *
  694. * @dev: Device that will be interacted with
  695. * @regmap: regmap bank in which this register field is located.
  696. * @reg_field: Register field with in the bank.
  697. *
  698. * The return value will be an ERR_PTR() on error or a valid pointer
  699. * to a struct regmap_field. The regmap_field will be automatically freed
  700. * by the device management code.
  701. */
  702. struct regmap_field *devm_regmap_field_alloc(struct device *dev,
  703. struct regmap *regmap, struct reg_field reg_field)
  704. {
  705. struct regmap_field *rm_field = devm_kzalloc(dev,
  706. sizeof(*rm_field), GFP_KERNEL);
  707. if (!rm_field)
  708. return ERR_PTR(-ENOMEM);
  709. regmap_field_init(rm_field, regmap, reg_field);
  710. return rm_field;
  711. }
  712. EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
  713. /**
  714. * devm_regmap_field_free(): Free register field allocated using
  715. * devm_regmap_field_alloc. Usally drivers need not call this function,
  716. * as the memory allocated via devm will be freed as per device-driver
  717. * life-cyle.
  718. *
  719. * @dev: Device that will be interacted with
  720. * @field: regmap field which should be freed.
  721. */
  722. void devm_regmap_field_free(struct device *dev,
  723. struct regmap_field *field)
  724. {
  725. devm_kfree(dev, field);
  726. }
  727. EXPORT_SYMBOL_GPL(devm_regmap_field_free);
  728. /**
  729. * regmap_field_alloc(): Allocate and initialise a register field
  730. * in a register map.
  731. *
  732. * @regmap: regmap bank in which this register field is located.
  733. * @reg_field: Register field with in the bank.
  734. *
  735. * The return value will be an ERR_PTR() on error or a valid pointer
  736. * to a struct regmap_field. The regmap_field should be freed by the
  737. * user once its finished working with it using regmap_field_free().
  738. */
  739. struct regmap_field *regmap_field_alloc(struct regmap *regmap,
  740. struct reg_field reg_field)
  741. {
  742. struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
  743. if (!rm_field)
  744. return ERR_PTR(-ENOMEM);
  745. regmap_field_init(rm_field, regmap, reg_field);
  746. return rm_field;
  747. }
  748. EXPORT_SYMBOL_GPL(regmap_field_alloc);
  749. /**
  750. * regmap_field_free(): Free register field allocated using regmap_field_alloc
  751. *
  752. * @field: regmap field which should be freed.
  753. */
  754. void regmap_field_free(struct regmap_field *field)
  755. {
  756. kfree(field);
  757. }
  758. EXPORT_SYMBOL_GPL(regmap_field_free);
  759. /**
  760. * regmap_reinit_cache(): Reinitialise the current register cache
  761. *
  762. * @map: Register map to operate on.
  763. * @config: New configuration. Only the cache data will be used.
  764. *
  765. * Discard any existing register cache for the map and initialize a
  766. * new cache. This can be used to restore the cache to defaults or to
  767. * update the cache configuration to reflect runtime discovery of the
  768. * hardware.
  769. *
  770. * No explicit locking is done here, the user needs to ensure that
  771. * this function will not race with other calls to regmap.
  772. */
  773. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  774. {
  775. regcache_exit(map);
  776. regmap_debugfs_exit(map);
  777. map->max_register = config->max_register;
  778. map->writeable_reg = config->writeable_reg;
  779. map->readable_reg = config->readable_reg;
  780. map->volatile_reg = config->volatile_reg;
  781. map->precious_reg = config->precious_reg;
  782. map->cache_type = config->cache_type;
  783. regmap_debugfs_init(map, config->name);
  784. map->cache_bypass = false;
  785. map->cache_only = false;
  786. return regcache_init(map, config);
  787. }
  788. EXPORT_SYMBOL_GPL(regmap_reinit_cache);
  789. /**
  790. * regmap_exit(): Free a previously allocated register map
  791. */
  792. void regmap_exit(struct regmap *map)
  793. {
  794. regcache_exit(map);
  795. regmap_debugfs_exit(map);
  796. regmap_range_exit(map);
  797. if (map->bus && map->bus->free_context)
  798. map->bus->free_context(map->bus_context);
  799. kfree(map->work_buf);
  800. kfree(map);
  801. }
  802. EXPORT_SYMBOL_GPL(regmap_exit);
  803. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  804. {
  805. struct regmap **r = res;
  806. if (!r || !*r) {
  807. WARN_ON(!r || !*r);
  808. return 0;
  809. }
  810. /* If the user didn't specify a name match any */
  811. if (data)
  812. return (*r)->name == data;
  813. else
  814. return 1;
  815. }
  816. /**
  817. * dev_get_regmap(): Obtain the regmap (if any) for a device
  818. *
  819. * @dev: Device to retrieve the map for
  820. * @name: Optional name for the register map, usually NULL.
  821. *
  822. * Returns the regmap for the device if one is present, or NULL. If
  823. * name is specified then it must match the name specified when
  824. * registering the device, if it is NULL then the first regmap found
  825. * will be used. Devices with multiple register maps are very rare,
  826. * generic code should normally not need to specify a name.
  827. */
  828. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  829. {
  830. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  831. dev_get_regmap_match, (void *)name);
  832. if (!r)
  833. return NULL;
  834. return *r;
  835. }
  836. EXPORT_SYMBOL_GPL(dev_get_regmap);
  837. static int _regmap_select_page(struct regmap *map, unsigned int *reg,
  838. struct regmap_range_node *range,
  839. unsigned int val_num)
  840. {
  841. void *orig_work_buf;
  842. unsigned int win_offset;
  843. unsigned int win_page;
  844. bool page_chg;
  845. int ret;
  846. win_offset = (*reg - range->range_min) % range->window_len;
  847. win_page = (*reg - range->range_min) / range->window_len;
  848. if (val_num > 1) {
  849. /* Bulk write shouldn't cross range boundary */
  850. if (*reg + val_num - 1 > range->range_max)
  851. return -EINVAL;
  852. /* ... or single page boundary */
  853. if (val_num > range->window_len - win_offset)
  854. return -EINVAL;
  855. }
  856. /* It is possible to have selector register inside data window.
  857. In that case, selector register is located on every page and
  858. it needs no page switching, when accessed alone. */
  859. if (val_num > 1 ||
  860. range->window_start + win_offset != range->selector_reg) {
  861. /* Use separate work_buf during page switching */
  862. orig_work_buf = map->work_buf;
  863. map->work_buf = map->selector_work_buf;
  864. ret = _regmap_update_bits(map, range->selector_reg,
  865. range->selector_mask,
  866. win_page << range->selector_shift,
  867. &page_chg);
  868. map->work_buf = orig_work_buf;
  869. if (ret != 0)
  870. return ret;
  871. }
  872. *reg = range->window_start + win_offset;
  873. return 0;
  874. }
  875. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  876. const void *val, size_t val_len, bool async)
  877. {
  878. struct regmap_range_node *range;
  879. unsigned long flags;
  880. u8 *u8 = map->work_buf;
  881. void *work_val = map->work_buf + map->format.reg_bytes +
  882. map->format.pad_bytes;
  883. void *buf;
  884. int ret = -ENOTSUPP;
  885. size_t len;
  886. int i;
  887. WARN_ON(!map->bus);
  888. /* Check for unwritable registers before we start */
  889. if (map->writeable_reg)
  890. for (i = 0; i < val_len / map->format.val_bytes; i++)
  891. if (!map->writeable_reg(map->dev,
  892. reg + (i * map->reg_stride)))
  893. return -EINVAL;
  894. if (!map->cache_bypass && map->format.parse_val) {
  895. unsigned int ival;
  896. int val_bytes = map->format.val_bytes;
  897. for (i = 0; i < val_len / val_bytes; i++) {
  898. ival = map->format.parse_val(val + (i * val_bytes));
  899. ret = regcache_write(map, reg + (i * map->reg_stride),
  900. ival);
  901. if (ret) {
  902. dev_err(map->dev,
  903. "Error in caching of register: %x ret: %d\n",
  904. reg + i, ret);
  905. return ret;
  906. }
  907. }
  908. if (map->cache_only) {
  909. map->cache_dirty = true;
  910. return 0;
  911. }
  912. }
  913. range = _regmap_range_lookup(map, reg);
  914. if (range) {
  915. int val_num = val_len / map->format.val_bytes;
  916. int win_offset = (reg - range->range_min) % range->window_len;
  917. int win_residue = range->window_len - win_offset;
  918. /* If the write goes beyond the end of the window split it */
  919. while (val_num > win_residue) {
  920. dev_dbg(map->dev, "Writing window %d/%zu\n",
  921. win_residue, val_len / map->format.val_bytes);
  922. ret = _regmap_raw_write(map, reg, val, win_residue *
  923. map->format.val_bytes, async);
  924. if (ret != 0)
  925. return ret;
  926. reg += win_residue;
  927. val_num -= win_residue;
  928. val += win_residue * map->format.val_bytes;
  929. val_len -= win_residue * map->format.val_bytes;
  930. win_offset = (reg - range->range_min) %
  931. range->window_len;
  932. win_residue = range->window_len - win_offset;
  933. }
  934. ret = _regmap_select_page(map, &reg, range, val_num);
  935. if (ret != 0)
  936. return ret;
  937. }
  938. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  939. u8[0] |= map->write_flag_mask;
  940. if (async && map->bus->async_write) {
  941. struct regmap_async *async = map->bus->async_alloc();
  942. if (!async)
  943. return -ENOMEM;
  944. trace_regmap_async_write_start(map->dev, reg, val_len);
  945. async->work_buf = kzalloc(map->format.buf_size,
  946. GFP_KERNEL | GFP_DMA);
  947. if (!async->work_buf) {
  948. kfree(async);
  949. return -ENOMEM;
  950. }
  951. INIT_WORK(&async->cleanup, async_cleanup);
  952. async->map = map;
  953. /* If the caller supplied the value we can use it safely. */
  954. memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
  955. map->format.reg_bytes + map->format.val_bytes);
  956. if (val == work_val)
  957. val = async->work_buf + map->format.pad_bytes +
  958. map->format.reg_bytes;
  959. spin_lock_irqsave(&map->async_lock, flags);
  960. list_add_tail(&async->list, &map->async_list);
  961. spin_unlock_irqrestore(&map->async_lock, flags);
  962. ret = map->bus->async_write(map->bus_context, async->work_buf,
  963. map->format.reg_bytes +
  964. map->format.pad_bytes,
  965. val, val_len, async);
  966. if (ret != 0) {
  967. dev_err(map->dev, "Failed to schedule write: %d\n",
  968. ret);
  969. spin_lock_irqsave(&map->async_lock, flags);
  970. list_del(&async->list);
  971. spin_unlock_irqrestore(&map->async_lock, flags);
  972. kfree(async->work_buf);
  973. kfree(async);
  974. }
  975. return ret;
  976. }
  977. trace_regmap_hw_write_start(map->dev, reg,
  978. val_len / map->format.val_bytes);
  979. /* If we're doing a single register write we can probably just
  980. * send the work_buf directly, otherwise try to do a gather
  981. * write.
  982. */
  983. if (val == work_val)
  984. ret = map->bus->write(map->bus_context, map->work_buf,
  985. map->format.reg_bytes +
  986. map->format.pad_bytes +
  987. val_len);
  988. else if (map->bus->gather_write)
  989. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  990. map->format.reg_bytes +
  991. map->format.pad_bytes,
  992. val, val_len);
  993. /* If that didn't work fall back on linearising by hand. */
  994. if (ret == -ENOTSUPP) {
  995. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  996. buf = kzalloc(len, GFP_KERNEL);
  997. if (!buf)
  998. return -ENOMEM;
  999. memcpy(buf, map->work_buf, map->format.reg_bytes);
  1000. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  1001. val, val_len);
  1002. ret = map->bus->write(map->bus_context, buf, len);
  1003. kfree(buf);
  1004. }
  1005. trace_regmap_hw_write_done(map->dev, reg,
  1006. val_len / map->format.val_bytes);
  1007. return ret;
  1008. }
  1009. /**
  1010. * regmap_can_raw_write - Test if regmap_raw_write() is supported
  1011. *
  1012. * @map: Map to check.
  1013. */
  1014. bool regmap_can_raw_write(struct regmap *map)
  1015. {
  1016. return map->bus && map->format.format_val && map->format.format_reg;
  1017. }
  1018. EXPORT_SYMBOL_GPL(regmap_can_raw_write);
  1019. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  1020. unsigned int val)
  1021. {
  1022. int ret;
  1023. struct regmap_range_node *range;
  1024. struct regmap *map = context;
  1025. WARN_ON(!map->bus || !map->format.format_write);
  1026. range = _regmap_range_lookup(map, reg);
  1027. if (range) {
  1028. ret = _regmap_select_page(map, &reg, range, 1);
  1029. if (ret != 0)
  1030. return ret;
  1031. }
  1032. map->format.format_write(map, reg, val);
  1033. trace_regmap_hw_write_start(map->dev, reg, 1);
  1034. ret = map->bus->write(map->bus_context, map->work_buf,
  1035. map->format.buf_size);
  1036. trace_regmap_hw_write_done(map->dev, reg, 1);
  1037. return ret;
  1038. }
  1039. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  1040. unsigned int val)
  1041. {
  1042. struct regmap *map = context;
  1043. WARN_ON(!map->bus || !map->format.format_val);
  1044. map->format.format_val(map->work_buf + map->format.reg_bytes
  1045. + map->format.pad_bytes, val, 0);
  1046. return _regmap_raw_write(map, reg,
  1047. map->work_buf +
  1048. map->format.reg_bytes +
  1049. map->format.pad_bytes,
  1050. map->format.val_bytes, false);
  1051. }
  1052. static inline void *_regmap_map_get_context(struct regmap *map)
  1053. {
  1054. return (map->bus) ? map : map->bus_context;
  1055. }
  1056. int _regmap_write(struct regmap *map, unsigned int reg,
  1057. unsigned int val)
  1058. {
  1059. int ret;
  1060. void *context = _regmap_map_get_context(map);
  1061. if (!map->cache_bypass && !map->defer_caching) {
  1062. ret = regcache_write(map, reg, val);
  1063. if (ret != 0)
  1064. return ret;
  1065. if (map->cache_only) {
  1066. map->cache_dirty = true;
  1067. return 0;
  1068. }
  1069. }
  1070. #ifdef LOG_DEVICE
  1071. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1072. dev_info(map->dev, "%x <= %x\n", reg, val);
  1073. #endif
  1074. trace_regmap_reg_write(map->dev, reg, val);
  1075. return map->reg_write(context, reg, val);
  1076. }
  1077. /**
  1078. * regmap_write(): Write a value to a single register
  1079. *
  1080. * @map: Register map to write to
  1081. * @reg: Register to write to
  1082. * @val: Value to be written
  1083. *
  1084. * A value of zero will be returned on success, a negative errno will
  1085. * be returned in error cases.
  1086. */
  1087. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  1088. {
  1089. int ret;
  1090. if (reg % map->reg_stride)
  1091. return -EINVAL;
  1092. map->lock(map->lock_arg);
  1093. ret = _regmap_write(map, reg, val);
  1094. map->unlock(map->lock_arg);
  1095. return ret;
  1096. }
  1097. EXPORT_SYMBOL_GPL(regmap_write);
  1098. /**
  1099. * regmap_raw_write(): Write raw values to one or more registers
  1100. *
  1101. * @map: Register map to write to
  1102. * @reg: Initial register to write to
  1103. * @val: Block of data to be written, laid out for direct transmission to the
  1104. * device
  1105. * @val_len: Length of data pointed to by val.
  1106. *
  1107. * This function is intended to be used for things like firmware
  1108. * download where a large block of data needs to be transferred to the
  1109. * device. No formatting will be done on the data provided.
  1110. *
  1111. * A value of zero will be returned on success, a negative errno will
  1112. * be returned in error cases.
  1113. */
  1114. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1115. const void *val, size_t val_len)
  1116. {
  1117. int ret;
  1118. if (!regmap_can_raw_write(map))
  1119. return -EINVAL;
  1120. if (val_len % map->format.val_bytes)
  1121. return -EINVAL;
  1122. map->lock(map->lock_arg);
  1123. ret = _regmap_raw_write(map, reg, val, val_len, false);
  1124. map->unlock(map->lock_arg);
  1125. return ret;
  1126. }
  1127. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1128. /**
  1129. * regmap_field_write(): Write a value to a single register field
  1130. *
  1131. * @field: Register field to write to
  1132. * @val: Value to be written
  1133. *
  1134. * A value of zero will be returned on success, a negative errno will
  1135. * be returned in error cases.
  1136. */
  1137. int regmap_field_write(struct regmap_field *field, unsigned int val)
  1138. {
  1139. return regmap_update_bits(field->regmap, field->reg,
  1140. field->mask, val << field->shift);
  1141. }
  1142. EXPORT_SYMBOL_GPL(regmap_field_write);
  1143. /*
  1144. * regmap_bulk_write(): Write multiple registers to the device
  1145. *
  1146. * @map: Register map to write to
  1147. * @reg: First register to be write from
  1148. * @val: Block of data to be written, in native register size for device
  1149. * @val_count: Number of registers to write
  1150. *
  1151. * This function is intended to be used for writing a large block of
  1152. * data to the device either in single transfer or multiple transfer.
  1153. *
  1154. * A value of zero will be returned on success, a negative errno will
  1155. * be returned in error cases.
  1156. */
  1157. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1158. size_t val_count)
  1159. {
  1160. int ret = 0, i;
  1161. size_t val_bytes = map->format.val_bytes;
  1162. void *wval;
  1163. if (!map->bus)
  1164. return -EINVAL;
  1165. if (!map->format.parse_inplace)
  1166. return -EINVAL;
  1167. if (reg % map->reg_stride)
  1168. return -EINVAL;
  1169. map->lock(map->lock_arg);
  1170. /* No formatting is require if val_byte is 1 */
  1171. if (val_bytes == 1) {
  1172. wval = (void *)val;
  1173. } else {
  1174. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  1175. if (!wval) {
  1176. ret = -ENOMEM;
  1177. dev_err(map->dev, "Error in memory allocation\n");
  1178. goto out;
  1179. }
  1180. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1181. map->format.parse_inplace(wval + i);
  1182. }
  1183. /*
  1184. * Some devices does not support bulk write, for
  1185. * them we have a series of single write operations.
  1186. */
  1187. if (map->use_single_rw) {
  1188. for (i = 0; i < val_count; i++) {
  1189. ret = regmap_raw_write(map,
  1190. reg + (i * map->reg_stride),
  1191. val + (i * val_bytes),
  1192. val_bytes);
  1193. if (ret != 0)
  1194. return ret;
  1195. }
  1196. } else {
  1197. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count,
  1198. false);
  1199. }
  1200. if (val_bytes != 1)
  1201. kfree(wval);
  1202. out:
  1203. map->unlock(map->lock_arg);
  1204. return ret;
  1205. }
  1206. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1207. /**
  1208. * regmap_raw_write_async(): Write raw values to one or more registers
  1209. * asynchronously
  1210. *
  1211. * @map: Register map to write to
  1212. * @reg: Initial register to write to
  1213. * @val: Block of data to be written, laid out for direct transmission to the
  1214. * device. Must be valid until regmap_async_complete() is called.
  1215. * @val_len: Length of data pointed to by val.
  1216. *
  1217. * This function is intended to be used for things like firmware
  1218. * download where a large block of data needs to be transferred to the
  1219. * device. No formatting will be done on the data provided.
  1220. *
  1221. * If supported by the underlying bus the write will be scheduled
  1222. * asynchronously, helping maximise I/O speed on higher speed buses
  1223. * like SPI. regmap_async_complete() can be called to ensure that all
  1224. * asynchrnous writes have been completed.
  1225. *
  1226. * A value of zero will be returned on success, a negative errno will
  1227. * be returned in error cases.
  1228. */
  1229. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  1230. const void *val, size_t val_len)
  1231. {
  1232. int ret;
  1233. if (val_len % map->format.val_bytes)
  1234. return -EINVAL;
  1235. if (reg % map->reg_stride)
  1236. return -EINVAL;
  1237. map->lock(map->lock_arg);
  1238. ret = _regmap_raw_write(map, reg, val, val_len, true);
  1239. map->unlock(map->lock_arg);
  1240. return ret;
  1241. }
  1242. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  1243. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1244. unsigned int val_len)
  1245. {
  1246. struct regmap_range_node *range;
  1247. u8 *u8 = map->work_buf;
  1248. int ret;
  1249. WARN_ON(!map->bus);
  1250. range = _regmap_range_lookup(map, reg);
  1251. if (range) {
  1252. ret = _regmap_select_page(map, &reg, range,
  1253. val_len / map->format.val_bytes);
  1254. if (ret != 0)
  1255. return ret;
  1256. }
  1257. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1258. /*
  1259. * Some buses or devices flag reads by setting the high bits in the
  1260. * register addresss; since it's always the high bits for all
  1261. * current formats we can do this here rather than in
  1262. * formatting. This may break if we get interesting formats.
  1263. */
  1264. u8[0] |= map->read_flag_mask;
  1265. trace_regmap_hw_read_start(map->dev, reg,
  1266. val_len / map->format.val_bytes);
  1267. ret = map->bus->read(map->bus_context, map->work_buf,
  1268. map->format.reg_bytes + map->format.pad_bytes,
  1269. val, val_len);
  1270. trace_regmap_hw_read_done(map->dev, reg,
  1271. val_len / map->format.val_bytes);
  1272. return ret;
  1273. }
  1274. static int _regmap_bus_read(void *context, unsigned int reg,
  1275. unsigned int *val)
  1276. {
  1277. int ret;
  1278. struct regmap *map = context;
  1279. if (!map->format.parse_val)
  1280. return -EINVAL;
  1281. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  1282. if (ret == 0)
  1283. *val = map->format.parse_val(map->work_buf);
  1284. return ret;
  1285. }
  1286. static int _regmap_read(struct regmap *map, unsigned int reg,
  1287. unsigned int *val)
  1288. {
  1289. int ret;
  1290. void *context = _regmap_map_get_context(map);
  1291. WARN_ON(!map->reg_read);
  1292. if (!map->cache_bypass) {
  1293. ret = regcache_read(map, reg, val);
  1294. if (ret == 0)
  1295. return 0;
  1296. }
  1297. if (map->cache_only)
  1298. return -EBUSY;
  1299. ret = map->reg_read(context, reg, val);
  1300. if (ret == 0) {
  1301. #ifdef LOG_DEVICE
  1302. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1303. dev_info(map->dev, "%x => %x\n", reg, *val);
  1304. #endif
  1305. trace_regmap_reg_read(map->dev, reg, *val);
  1306. if (!map->cache_bypass)
  1307. regcache_write(map, reg, *val);
  1308. }
  1309. return ret;
  1310. }
  1311. /**
  1312. * regmap_read(): Read a value from a single register
  1313. *
  1314. * @map: Register map to write to
  1315. * @reg: Register to be read from
  1316. * @val: Pointer to store read value
  1317. *
  1318. * A value of zero will be returned on success, a negative errno will
  1319. * be returned in error cases.
  1320. */
  1321. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  1322. {
  1323. int ret;
  1324. if (reg % map->reg_stride)
  1325. return -EINVAL;
  1326. map->lock(map->lock_arg);
  1327. ret = _regmap_read(map, reg, val);
  1328. map->unlock(map->lock_arg);
  1329. return ret;
  1330. }
  1331. EXPORT_SYMBOL_GPL(regmap_read);
  1332. /**
  1333. * regmap_raw_read(): Read raw data from the device
  1334. *
  1335. * @map: Register map to write to
  1336. * @reg: First register to be read from
  1337. * @val: Pointer to store read value
  1338. * @val_len: Size of data to read
  1339. *
  1340. * A value of zero will be returned on success, a negative errno will
  1341. * be returned in error cases.
  1342. */
  1343. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1344. size_t val_len)
  1345. {
  1346. size_t val_bytes = map->format.val_bytes;
  1347. size_t val_count = val_len / val_bytes;
  1348. unsigned int v;
  1349. int ret, i;
  1350. if (!map->bus)
  1351. return -EINVAL;
  1352. if (val_len % map->format.val_bytes)
  1353. return -EINVAL;
  1354. if (reg % map->reg_stride)
  1355. return -EINVAL;
  1356. map->lock(map->lock_arg);
  1357. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  1358. map->cache_type == REGCACHE_NONE) {
  1359. /* Physical block read if there's no cache involved */
  1360. ret = _regmap_raw_read(map, reg, val, val_len);
  1361. } else {
  1362. /* Otherwise go word by word for the cache; should be low
  1363. * cost as we expect to hit the cache.
  1364. */
  1365. for (i = 0; i < val_count; i++) {
  1366. ret = _regmap_read(map, reg + (i * map->reg_stride),
  1367. &v);
  1368. if (ret != 0)
  1369. goto out;
  1370. map->format.format_val(val + (i * val_bytes), v, 0);
  1371. }
  1372. }
  1373. out:
  1374. map->unlock(map->lock_arg);
  1375. return ret;
  1376. }
  1377. EXPORT_SYMBOL_GPL(regmap_raw_read);
  1378. /**
  1379. * regmap_field_read(): Read a value to a single register field
  1380. *
  1381. * @field: Register field to read from
  1382. * @val: Pointer to store read value
  1383. *
  1384. * A value of zero will be returned on success, a negative errno will
  1385. * be returned in error cases.
  1386. */
  1387. int regmap_field_read(struct regmap_field *field, unsigned int *val)
  1388. {
  1389. int ret;
  1390. unsigned int reg_val;
  1391. ret = regmap_read(field->regmap, field->reg, &reg_val);
  1392. if (ret != 0)
  1393. return ret;
  1394. reg_val &= field->mask;
  1395. reg_val >>= field->shift;
  1396. *val = reg_val;
  1397. return ret;
  1398. }
  1399. EXPORT_SYMBOL_GPL(regmap_field_read);
  1400. /**
  1401. * regmap_bulk_read(): Read multiple registers from the device
  1402. *
  1403. * @map: Register map to write to
  1404. * @reg: First register to be read from
  1405. * @val: Pointer to store read value, in native register size for device
  1406. * @val_count: Number of registers to read
  1407. *
  1408. * A value of zero will be returned on success, a negative errno will
  1409. * be returned in error cases.
  1410. */
  1411. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  1412. size_t val_count)
  1413. {
  1414. int ret, i;
  1415. size_t val_bytes = map->format.val_bytes;
  1416. bool vol = regmap_volatile_range(map, reg, val_count);
  1417. if (!map->bus)
  1418. return -EINVAL;
  1419. if (!map->format.parse_inplace)
  1420. return -EINVAL;
  1421. if (reg % map->reg_stride)
  1422. return -EINVAL;
  1423. if (vol || map->cache_type == REGCACHE_NONE) {
  1424. /*
  1425. * Some devices does not support bulk read, for
  1426. * them we have a series of single read operations.
  1427. */
  1428. if (map->use_single_rw) {
  1429. for (i = 0; i < val_count; i++) {
  1430. ret = regmap_raw_read(map,
  1431. reg + (i * map->reg_stride),
  1432. val + (i * val_bytes),
  1433. val_bytes);
  1434. if (ret != 0)
  1435. return ret;
  1436. }
  1437. } else {
  1438. ret = regmap_raw_read(map, reg, val,
  1439. val_bytes * val_count);
  1440. if (ret != 0)
  1441. return ret;
  1442. }
  1443. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1444. map->format.parse_inplace(val + i);
  1445. } else {
  1446. for (i = 0; i < val_count; i++) {
  1447. unsigned int ival;
  1448. ret = regmap_read(map, reg + (i * map->reg_stride),
  1449. &ival);
  1450. if (ret != 0)
  1451. return ret;
  1452. memcpy(val + (i * val_bytes), &ival, val_bytes);
  1453. }
  1454. }
  1455. return 0;
  1456. }
  1457. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  1458. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  1459. unsigned int mask, unsigned int val,
  1460. bool *change)
  1461. {
  1462. int ret;
  1463. unsigned int tmp, orig;
  1464. ret = _regmap_read(map, reg, &orig);
  1465. if (ret != 0)
  1466. return ret;
  1467. tmp = orig & ~mask;
  1468. tmp |= val & mask;
  1469. if (tmp != orig) {
  1470. ret = _regmap_write(map, reg, tmp);
  1471. *change = true;
  1472. } else {
  1473. *change = false;
  1474. }
  1475. return ret;
  1476. }
  1477. /**
  1478. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  1479. *
  1480. * @map: Register map to update
  1481. * @reg: Register to update
  1482. * @mask: Bitmask to change
  1483. * @val: New value for bitmask
  1484. *
  1485. * Returns zero for success, a negative number on error.
  1486. */
  1487. int regmap_update_bits(struct regmap *map, unsigned int reg,
  1488. unsigned int mask, unsigned int val)
  1489. {
  1490. bool change;
  1491. int ret;
  1492. map->lock(map->lock_arg);
  1493. ret = _regmap_update_bits(map, reg, mask, val, &change);
  1494. map->unlock(map->lock_arg);
  1495. return ret;
  1496. }
  1497. EXPORT_SYMBOL_GPL(regmap_update_bits);
  1498. /**
  1499. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  1500. * register map and report if updated
  1501. *
  1502. * @map: Register map to update
  1503. * @reg: Register to update
  1504. * @mask: Bitmask to change
  1505. * @val: New value for bitmask
  1506. * @change: Boolean indicating if a write was done
  1507. *
  1508. * Returns zero for success, a negative number on error.
  1509. */
  1510. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  1511. unsigned int mask, unsigned int val,
  1512. bool *change)
  1513. {
  1514. int ret;
  1515. map->lock(map->lock_arg);
  1516. ret = _regmap_update_bits(map, reg, mask, val, change);
  1517. map->unlock(map->lock_arg);
  1518. return ret;
  1519. }
  1520. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  1521. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  1522. {
  1523. struct regmap *map = async->map;
  1524. bool wake;
  1525. trace_regmap_async_io_complete(map->dev);
  1526. spin_lock(&map->async_lock);
  1527. list_del(&async->list);
  1528. wake = list_empty(&map->async_list);
  1529. if (ret != 0)
  1530. map->async_ret = ret;
  1531. spin_unlock(&map->async_lock);
  1532. schedule_work(&async->cleanup);
  1533. if (wake)
  1534. wake_up(&map->async_waitq);
  1535. }
  1536. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  1537. static int regmap_async_is_done(struct regmap *map)
  1538. {
  1539. unsigned long flags;
  1540. int ret;
  1541. spin_lock_irqsave(&map->async_lock, flags);
  1542. ret = list_empty(&map->async_list);
  1543. spin_unlock_irqrestore(&map->async_lock, flags);
  1544. return ret;
  1545. }
  1546. /**
  1547. * regmap_async_complete: Ensure all asynchronous I/O has completed.
  1548. *
  1549. * @map: Map to operate on.
  1550. *
  1551. * Blocks until any pending asynchronous I/O has completed. Returns
  1552. * an error code for any failed I/O operations.
  1553. */
  1554. int regmap_async_complete(struct regmap *map)
  1555. {
  1556. unsigned long flags;
  1557. int ret;
  1558. /* Nothing to do with no async support */
  1559. if (!map->bus || !map->bus->async_write)
  1560. return 0;
  1561. trace_regmap_async_complete_start(map->dev);
  1562. wait_event(map->async_waitq, regmap_async_is_done(map));
  1563. spin_lock_irqsave(&map->async_lock, flags);
  1564. ret = map->async_ret;
  1565. map->async_ret = 0;
  1566. spin_unlock_irqrestore(&map->async_lock, flags);
  1567. trace_regmap_async_complete_done(map->dev);
  1568. return ret;
  1569. }
  1570. EXPORT_SYMBOL_GPL(regmap_async_complete);
  1571. /**
  1572. * regmap_register_patch: Register and apply register updates to be applied
  1573. * on device initialistion
  1574. *
  1575. * @map: Register map to apply updates to.
  1576. * @regs: Values to update.
  1577. * @num_regs: Number of entries in regs.
  1578. *
  1579. * Register a set of register updates to be applied to the device
  1580. * whenever the device registers are synchronised with the cache and
  1581. * apply them immediately. Typically this is used to apply
  1582. * corrections to be applied to the device defaults on startup, such
  1583. * as the updates some vendors provide to undocumented registers.
  1584. */
  1585. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  1586. int num_regs)
  1587. {
  1588. int i, ret;
  1589. bool bypass;
  1590. /* If needed the implementation can be extended to support this */
  1591. if (map->patch)
  1592. return -EBUSY;
  1593. map->lock(map->lock_arg);
  1594. bypass = map->cache_bypass;
  1595. map->cache_bypass = true;
  1596. /* Write out first; it's useful to apply even if we fail later. */
  1597. for (i = 0; i < num_regs; i++) {
  1598. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1599. if (ret != 0) {
  1600. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  1601. regs[i].reg, regs[i].def, ret);
  1602. goto out;
  1603. }
  1604. }
  1605. map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
  1606. if (map->patch != NULL) {
  1607. memcpy(map->patch, regs,
  1608. num_regs * sizeof(struct reg_default));
  1609. map->patch_regs = num_regs;
  1610. } else {
  1611. ret = -ENOMEM;
  1612. }
  1613. out:
  1614. map->cache_bypass = bypass;
  1615. map->unlock(map->lock_arg);
  1616. return ret;
  1617. }
  1618. EXPORT_SYMBOL_GPL(regmap_register_patch);
  1619. /*
  1620. * regmap_get_val_bytes(): Report the size of a register value
  1621. *
  1622. * Report the size of a register value, mainly intended to for use by
  1623. * generic infrastructure built on top of regmap.
  1624. */
  1625. int regmap_get_val_bytes(struct regmap *map)
  1626. {
  1627. if (map->format.format_write)
  1628. return -EINVAL;
  1629. return map->format.val_bytes;
  1630. }
  1631. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  1632. static int __init regmap_initcall(void)
  1633. {
  1634. regmap_debugfs_initcall();
  1635. return 0;
  1636. }
  1637. postcore_initcall(regmap_initcall);