regmap.c 48 KB

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