regmap.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. #define CREATE_TRACE_POINTS
  19. #include <trace/events/regmap.h>
  20. #include "internal.h"
  21. /*
  22. * Sometimes for failures during very early init the trace
  23. * infrastructure isn't available early enough to be used. For this
  24. * sort of problem defining LOG_DEVICE will add printks for basic
  25. * register I/O on a specific device.
  26. */
  27. #undef LOG_DEVICE
  28. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  29. unsigned int mask, unsigned int val,
  30. bool *change);
  31. static int _regmap_bus_read(void *context, unsigned int reg,
  32. unsigned int *val);
  33. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  34. unsigned int val);
  35. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  36. unsigned int val);
  37. bool regmap_reg_in_ranges(unsigned int reg,
  38. const struct regmap_range *ranges,
  39. unsigned int nranges)
  40. {
  41. const struct regmap_range *r;
  42. int i;
  43. for (i = 0, r = ranges; i < nranges; i++, r++)
  44. if (regmap_reg_in_range(reg, r))
  45. return true;
  46. return false;
  47. }
  48. EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
  49. static bool _regmap_check_range_table(struct regmap *map,
  50. 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. bool regmap_writeable(struct regmap *map, unsigned int reg)
  63. {
  64. if (map->max_register && reg > map->max_register)
  65. return false;
  66. if (map->writeable_reg)
  67. return map->writeable_reg(map->dev, reg);
  68. if (map->wr_table)
  69. return _regmap_check_range_table(map, reg, map->wr_table);
  70. return true;
  71. }
  72. bool regmap_readable(struct regmap *map, unsigned int reg)
  73. {
  74. if (map->max_register && reg > map->max_register)
  75. return false;
  76. if (map->format.format_write)
  77. return false;
  78. if (map->readable_reg)
  79. return map->readable_reg(map->dev, reg);
  80. if (map->rd_table)
  81. return _regmap_check_range_table(map, reg, map->rd_table);
  82. return true;
  83. }
  84. bool regmap_volatile(struct regmap *map, unsigned int reg)
  85. {
  86. if (!regmap_readable(map, reg))
  87. return false;
  88. if (map->volatile_reg)
  89. return map->volatile_reg(map->dev, reg);
  90. if (map->volatile_table)
  91. return _regmap_check_range_table(map, reg, map->volatile_table);
  92. return true;
  93. }
  94. bool regmap_precious(struct regmap *map, unsigned int reg)
  95. {
  96. if (!regmap_readable(map, reg))
  97. return false;
  98. if (map->precious_reg)
  99. return map->precious_reg(map->dev, reg);
  100. if (map->precious_table)
  101. return _regmap_check_range_table(map, reg, map->precious_table);
  102. return false;
  103. }
  104. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  105. size_t num)
  106. {
  107. unsigned int i;
  108. for (i = 0; i < num; i++)
  109. if (!regmap_volatile(map, reg + i))
  110. return false;
  111. return true;
  112. }
  113. static void regmap_format_2_6_write(struct regmap *map,
  114. unsigned int reg, unsigned int val)
  115. {
  116. u8 *out = map->work_buf;
  117. *out = (reg << 6) | val;
  118. }
  119. static void regmap_format_4_12_write(struct regmap *map,
  120. unsigned int reg, unsigned int val)
  121. {
  122. __be16 *out = map->work_buf;
  123. *out = cpu_to_be16((reg << 12) | val);
  124. }
  125. static void regmap_format_7_9_write(struct regmap *map,
  126. unsigned int reg, unsigned int val)
  127. {
  128. __be16 *out = map->work_buf;
  129. *out = cpu_to_be16((reg << 9) | val);
  130. }
  131. static void regmap_format_10_14_write(struct regmap *map,
  132. unsigned int reg, unsigned int val)
  133. {
  134. u8 *out = map->work_buf;
  135. out[2] = val;
  136. out[1] = (val >> 8) | (reg << 6);
  137. out[0] = reg >> 2;
  138. }
  139. static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
  140. {
  141. u8 *b = buf;
  142. b[0] = val << shift;
  143. }
  144. static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
  145. {
  146. __be16 *b = buf;
  147. b[0] = cpu_to_be16(val << shift);
  148. }
  149. static void regmap_format_16_native(void *buf, unsigned int val,
  150. unsigned int shift)
  151. {
  152. *(u16 *)buf = val << shift;
  153. }
  154. static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
  155. {
  156. u8 *b = buf;
  157. val <<= shift;
  158. b[0] = val >> 16;
  159. b[1] = val >> 8;
  160. b[2] = val;
  161. }
  162. static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
  163. {
  164. __be32 *b = buf;
  165. b[0] = cpu_to_be32(val << shift);
  166. }
  167. static void regmap_format_32_native(void *buf, unsigned int val,
  168. unsigned int shift)
  169. {
  170. *(u32 *)buf = val << shift;
  171. }
  172. static unsigned int regmap_parse_8(void *buf)
  173. {
  174. u8 *b = buf;
  175. return b[0];
  176. }
  177. static unsigned int regmap_parse_16_be(void *buf)
  178. {
  179. __be16 *b = buf;
  180. b[0] = be16_to_cpu(b[0]);
  181. return b[0];
  182. }
  183. static unsigned int regmap_parse_16_native(void *buf)
  184. {
  185. return *(u16 *)buf;
  186. }
  187. static unsigned int regmap_parse_24(void *buf)
  188. {
  189. u8 *b = buf;
  190. unsigned int ret = b[2];
  191. ret |= ((unsigned int)b[1]) << 8;
  192. ret |= ((unsigned int)b[0]) << 16;
  193. return ret;
  194. }
  195. static unsigned int regmap_parse_32_be(void *buf)
  196. {
  197. __be32 *b = buf;
  198. b[0] = be32_to_cpu(b[0]);
  199. return b[0];
  200. }
  201. static unsigned int regmap_parse_32_native(void *buf)
  202. {
  203. return *(u32 *)buf;
  204. }
  205. static void regmap_lock_mutex(void *__map)
  206. {
  207. struct regmap *map = __map;
  208. mutex_lock(&map->mutex);
  209. }
  210. static void regmap_unlock_mutex(void *__map)
  211. {
  212. struct regmap *map = __map;
  213. mutex_unlock(&map->mutex);
  214. }
  215. static void regmap_lock_spinlock(void *__map)
  216. {
  217. struct regmap *map = __map;
  218. spin_lock(&map->spinlock);
  219. }
  220. static void regmap_unlock_spinlock(void *__map)
  221. {
  222. struct regmap *map = __map;
  223. spin_unlock(&map->spinlock);
  224. }
  225. static void dev_get_regmap_release(struct device *dev, void *res)
  226. {
  227. /*
  228. * We don't actually have anything to do here; the goal here
  229. * is not to manage the regmap but to provide a simple way to
  230. * get the regmap back given a struct device.
  231. */
  232. }
  233. static bool _regmap_range_add(struct regmap *map,
  234. struct regmap_range_node *data)
  235. {
  236. struct rb_root *root = &map->range_tree;
  237. struct rb_node **new = &(root->rb_node), *parent = NULL;
  238. while (*new) {
  239. struct regmap_range_node *this =
  240. container_of(*new, struct regmap_range_node, node);
  241. parent = *new;
  242. if (data->range_max < this->range_min)
  243. new = &((*new)->rb_left);
  244. else if (data->range_min > this->range_max)
  245. new = &((*new)->rb_right);
  246. else
  247. return false;
  248. }
  249. rb_link_node(&data->node, parent, new);
  250. rb_insert_color(&data->node, root);
  251. return true;
  252. }
  253. static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
  254. unsigned int reg)
  255. {
  256. struct rb_node *node = map->range_tree.rb_node;
  257. while (node) {
  258. struct regmap_range_node *this =
  259. container_of(node, struct regmap_range_node, node);
  260. if (reg < this->range_min)
  261. node = node->rb_left;
  262. else if (reg > this->range_max)
  263. node = node->rb_right;
  264. else
  265. return this;
  266. }
  267. return NULL;
  268. }
  269. static void regmap_range_exit(struct regmap *map)
  270. {
  271. struct rb_node *next;
  272. struct regmap_range_node *range_node;
  273. next = rb_first(&map->range_tree);
  274. while (next) {
  275. range_node = rb_entry(next, struct regmap_range_node, node);
  276. next = rb_next(&range_node->node);
  277. rb_erase(&range_node->node, &map->range_tree);
  278. kfree(range_node);
  279. }
  280. kfree(map->selector_work_buf);
  281. }
  282. /**
  283. * regmap_init(): Initialise register map
  284. *
  285. * @dev: Device that will be interacted with
  286. * @bus: Bus-specific callbacks to use with device
  287. * @bus_context: Data passed to bus-specific callbacks
  288. * @config: Configuration for register map
  289. *
  290. * The return value will be an ERR_PTR() on error or a valid pointer to
  291. * a struct regmap. This function should generally not be called
  292. * directly, it should be called by bus-specific init functions.
  293. */
  294. struct regmap *regmap_init(struct device *dev,
  295. const struct regmap_bus *bus,
  296. void *bus_context,
  297. const struct regmap_config *config)
  298. {
  299. struct regmap *map, **m;
  300. int ret = -EINVAL;
  301. enum regmap_endian reg_endian, val_endian;
  302. int i, j;
  303. if (!bus || !config)
  304. goto err;
  305. map = kzalloc(sizeof(*map), GFP_KERNEL);
  306. if (map == NULL) {
  307. ret = -ENOMEM;
  308. goto err;
  309. }
  310. if (config->lock && config->unlock) {
  311. map->lock = config->lock;
  312. map->unlock = config->unlock;
  313. map->lock_arg = config->lock_arg;
  314. } else {
  315. if (bus->fast_io) {
  316. spin_lock_init(&map->spinlock);
  317. map->lock = regmap_lock_spinlock;
  318. map->unlock = regmap_unlock_spinlock;
  319. } else {
  320. mutex_init(&map->mutex);
  321. map->lock = regmap_lock_mutex;
  322. map->unlock = regmap_unlock_mutex;
  323. }
  324. map->lock_arg = map;
  325. }
  326. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  327. map->format.pad_bytes = config->pad_bits / 8;
  328. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  329. map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
  330. config->val_bits + config->pad_bits, 8);
  331. map->reg_shift = config->pad_bits % 8;
  332. if (config->reg_stride)
  333. map->reg_stride = config->reg_stride;
  334. else
  335. map->reg_stride = 1;
  336. map->use_single_rw = config->use_single_rw;
  337. map->dev = dev;
  338. map->bus = bus;
  339. map->bus_context = bus_context;
  340. map->max_register = config->max_register;
  341. map->wr_table = config->wr_table;
  342. map->rd_table = config->rd_table;
  343. map->volatile_table = config->volatile_table;
  344. map->precious_table = config->precious_table;
  345. map->writeable_reg = config->writeable_reg;
  346. map->readable_reg = config->readable_reg;
  347. map->volatile_reg = config->volatile_reg;
  348. map->precious_reg = config->precious_reg;
  349. map->cache_type = config->cache_type;
  350. map->name = config->name;
  351. if (config->read_flag_mask || config->write_flag_mask) {
  352. map->read_flag_mask = config->read_flag_mask;
  353. map->write_flag_mask = config->write_flag_mask;
  354. } else {
  355. map->read_flag_mask = bus->read_flag_mask;
  356. }
  357. map->reg_read = _regmap_bus_read;
  358. reg_endian = config->reg_format_endian;
  359. if (reg_endian == REGMAP_ENDIAN_DEFAULT)
  360. reg_endian = bus->reg_format_endian_default;
  361. if (reg_endian == REGMAP_ENDIAN_DEFAULT)
  362. reg_endian = REGMAP_ENDIAN_BIG;
  363. val_endian = config->val_format_endian;
  364. if (val_endian == REGMAP_ENDIAN_DEFAULT)
  365. val_endian = bus->val_format_endian_default;
  366. if (val_endian == REGMAP_ENDIAN_DEFAULT)
  367. val_endian = REGMAP_ENDIAN_BIG;
  368. switch (config->reg_bits + map->reg_shift) {
  369. case 2:
  370. switch (config->val_bits) {
  371. case 6:
  372. map->format.format_write = regmap_format_2_6_write;
  373. break;
  374. default:
  375. goto err_map;
  376. }
  377. break;
  378. case 4:
  379. switch (config->val_bits) {
  380. case 12:
  381. map->format.format_write = regmap_format_4_12_write;
  382. break;
  383. default:
  384. goto err_map;
  385. }
  386. break;
  387. case 7:
  388. switch (config->val_bits) {
  389. case 9:
  390. map->format.format_write = regmap_format_7_9_write;
  391. break;
  392. default:
  393. goto err_map;
  394. }
  395. break;
  396. case 10:
  397. switch (config->val_bits) {
  398. case 14:
  399. map->format.format_write = regmap_format_10_14_write;
  400. break;
  401. default:
  402. goto err_map;
  403. }
  404. break;
  405. case 8:
  406. map->format.format_reg = regmap_format_8;
  407. break;
  408. case 16:
  409. switch (reg_endian) {
  410. case REGMAP_ENDIAN_BIG:
  411. map->format.format_reg = regmap_format_16_be;
  412. break;
  413. case REGMAP_ENDIAN_NATIVE:
  414. map->format.format_reg = regmap_format_16_native;
  415. break;
  416. default:
  417. goto err_map;
  418. }
  419. break;
  420. case 32:
  421. switch (reg_endian) {
  422. case REGMAP_ENDIAN_BIG:
  423. map->format.format_reg = regmap_format_32_be;
  424. break;
  425. case REGMAP_ENDIAN_NATIVE:
  426. map->format.format_reg = regmap_format_32_native;
  427. break;
  428. default:
  429. goto err_map;
  430. }
  431. break;
  432. default:
  433. goto err_map;
  434. }
  435. switch (config->val_bits) {
  436. case 8:
  437. map->format.format_val = regmap_format_8;
  438. map->format.parse_val = regmap_parse_8;
  439. break;
  440. case 16:
  441. switch (val_endian) {
  442. case REGMAP_ENDIAN_BIG:
  443. map->format.format_val = regmap_format_16_be;
  444. map->format.parse_val = regmap_parse_16_be;
  445. break;
  446. case REGMAP_ENDIAN_NATIVE:
  447. map->format.format_val = regmap_format_16_native;
  448. map->format.parse_val = regmap_parse_16_native;
  449. break;
  450. default:
  451. goto err_map;
  452. }
  453. break;
  454. case 24:
  455. if (val_endian != REGMAP_ENDIAN_BIG)
  456. goto err_map;
  457. map->format.format_val = regmap_format_24;
  458. map->format.parse_val = regmap_parse_24;
  459. break;
  460. case 32:
  461. switch (val_endian) {
  462. case REGMAP_ENDIAN_BIG:
  463. map->format.format_val = regmap_format_32_be;
  464. map->format.parse_val = regmap_parse_32_be;
  465. break;
  466. case REGMAP_ENDIAN_NATIVE:
  467. map->format.format_val = regmap_format_32_native;
  468. map->format.parse_val = regmap_parse_32_native;
  469. break;
  470. default:
  471. goto err_map;
  472. }
  473. break;
  474. }
  475. if (map->format.format_write) {
  476. if ((reg_endian != REGMAP_ENDIAN_BIG) ||
  477. (val_endian != REGMAP_ENDIAN_BIG))
  478. goto err_map;
  479. map->use_single_rw = true;
  480. }
  481. if (!map->format.format_write &&
  482. !(map->format.format_reg && map->format.format_val))
  483. goto err_map;
  484. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  485. if (map->work_buf == NULL) {
  486. ret = -ENOMEM;
  487. goto err_map;
  488. }
  489. if (map->format.format_write)
  490. map->reg_write = _regmap_bus_formatted_write;
  491. else if (map->format.format_val)
  492. map->reg_write = _regmap_bus_raw_write;
  493. map->range_tree = RB_ROOT;
  494. for (i = 0; i < config->num_ranges; i++) {
  495. const struct regmap_range_cfg *range_cfg = &config->ranges[i];
  496. struct regmap_range_node *new;
  497. /* Sanity check */
  498. if (range_cfg->range_max < range_cfg->range_min) {
  499. dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
  500. range_cfg->range_max, range_cfg->range_min);
  501. goto err_range;
  502. }
  503. if (range_cfg->range_max > map->max_register) {
  504. dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
  505. range_cfg->range_max, map->max_register);
  506. goto err_range;
  507. }
  508. if (range_cfg->selector_reg > map->max_register) {
  509. dev_err(map->dev,
  510. "Invalid range %d: selector out of map\n", i);
  511. goto err_range;
  512. }
  513. if (range_cfg->window_len == 0) {
  514. dev_err(map->dev, "Invalid range %d: window_len 0\n",
  515. i);
  516. goto err_range;
  517. }
  518. /* Make sure, that this register range has no selector
  519. or data window within its boundary */
  520. for (j = 0; j < config->num_ranges; j++) {
  521. unsigned sel_reg = config->ranges[j].selector_reg;
  522. unsigned win_min = config->ranges[j].window_start;
  523. unsigned win_max = win_min +
  524. config->ranges[j].window_len - 1;
  525. if (range_cfg->range_min <= sel_reg &&
  526. sel_reg <= range_cfg->range_max) {
  527. dev_err(map->dev,
  528. "Range %d: selector for %d in window\n",
  529. i, j);
  530. goto err_range;
  531. }
  532. if (!(win_max < range_cfg->range_min ||
  533. win_min > range_cfg->range_max)) {
  534. dev_err(map->dev,
  535. "Range %d: window for %d in window\n",
  536. i, j);
  537. goto err_range;
  538. }
  539. }
  540. new = kzalloc(sizeof(*new), GFP_KERNEL);
  541. if (new == NULL) {
  542. ret = -ENOMEM;
  543. goto err_range;
  544. }
  545. new->map = map;
  546. new->name = range_cfg->name;
  547. new->range_min = range_cfg->range_min;
  548. new->range_max = range_cfg->range_max;
  549. new->selector_reg = range_cfg->selector_reg;
  550. new->selector_mask = range_cfg->selector_mask;
  551. new->selector_shift = range_cfg->selector_shift;
  552. new->window_start = range_cfg->window_start;
  553. new->window_len = range_cfg->window_len;
  554. if (_regmap_range_add(map, new) == false) {
  555. dev_err(map->dev, "Failed to add range %d\n", i);
  556. kfree(new);
  557. goto err_range;
  558. }
  559. if (map->selector_work_buf == NULL) {
  560. map->selector_work_buf =
  561. kzalloc(map->format.buf_size, GFP_KERNEL);
  562. if (map->selector_work_buf == NULL) {
  563. ret = -ENOMEM;
  564. goto err_range;
  565. }
  566. }
  567. }
  568. ret = regcache_init(map, config);
  569. if (ret != 0)
  570. goto err_range;
  571. regmap_debugfs_init(map, config->name);
  572. /* Add a devres resource for dev_get_regmap() */
  573. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  574. if (!m) {
  575. ret = -ENOMEM;
  576. goto err_debugfs;
  577. }
  578. *m = map;
  579. devres_add(dev, m);
  580. return map;
  581. err_debugfs:
  582. regmap_debugfs_exit(map);
  583. regcache_exit(map);
  584. err_range:
  585. regmap_range_exit(map);
  586. kfree(map->work_buf);
  587. err_map:
  588. kfree(map);
  589. err:
  590. return ERR_PTR(ret);
  591. }
  592. EXPORT_SYMBOL_GPL(regmap_init);
  593. static void devm_regmap_release(struct device *dev, void *res)
  594. {
  595. regmap_exit(*(struct regmap **)res);
  596. }
  597. /**
  598. * devm_regmap_init(): Initialise managed register map
  599. *
  600. * @dev: Device that will be interacted with
  601. * @bus: Bus-specific callbacks to use with device
  602. * @bus_context: Data passed to bus-specific callbacks
  603. * @config: Configuration for register map
  604. *
  605. * The return value will be an ERR_PTR() on error or a valid pointer
  606. * to a struct regmap. This function should generally not be called
  607. * directly, it should be called by bus-specific init functions. The
  608. * map will be automatically freed by the device management code.
  609. */
  610. struct regmap *devm_regmap_init(struct device *dev,
  611. const struct regmap_bus *bus,
  612. void *bus_context,
  613. const struct regmap_config *config)
  614. {
  615. struct regmap **ptr, *regmap;
  616. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  617. if (!ptr)
  618. return ERR_PTR(-ENOMEM);
  619. regmap = regmap_init(dev, bus, bus_context, config);
  620. if (!IS_ERR(regmap)) {
  621. *ptr = regmap;
  622. devres_add(dev, ptr);
  623. } else {
  624. devres_free(ptr);
  625. }
  626. return regmap;
  627. }
  628. EXPORT_SYMBOL_GPL(devm_regmap_init);
  629. /**
  630. * regmap_reinit_cache(): Reinitialise the current register cache
  631. *
  632. * @map: Register map to operate on.
  633. * @config: New configuration. Only the cache data will be used.
  634. *
  635. * Discard any existing register cache for the map and initialize a
  636. * new cache. This can be used to restore the cache to defaults or to
  637. * update the cache configuration to reflect runtime discovery of the
  638. * hardware.
  639. *
  640. * No explicit locking is done here, the user needs to ensure that
  641. * this function will not race with other calls to regmap.
  642. */
  643. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  644. {
  645. regcache_exit(map);
  646. regmap_debugfs_exit(map);
  647. map->max_register = config->max_register;
  648. map->writeable_reg = config->writeable_reg;
  649. map->readable_reg = config->readable_reg;
  650. map->volatile_reg = config->volatile_reg;
  651. map->precious_reg = config->precious_reg;
  652. map->cache_type = config->cache_type;
  653. regmap_debugfs_init(map, config->name);
  654. map->cache_bypass = false;
  655. map->cache_only = false;
  656. return regcache_init(map, config);
  657. }
  658. EXPORT_SYMBOL_GPL(regmap_reinit_cache);
  659. /**
  660. * regmap_exit(): Free a previously allocated register map
  661. */
  662. void regmap_exit(struct regmap *map)
  663. {
  664. regcache_exit(map);
  665. regmap_debugfs_exit(map);
  666. regmap_range_exit(map);
  667. if (map->bus->free_context)
  668. map->bus->free_context(map->bus_context);
  669. kfree(map->work_buf);
  670. kfree(map);
  671. }
  672. EXPORT_SYMBOL_GPL(regmap_exit);
  673. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  674. {
  675. struct regmap **r = res;
  676. if (!r || !*r) {
  677. WARN_ON(!r || !*r);
  678. return 0;
  679. }
  680. /* If the user didn't specify a name match any */
  681. if (data)
  682. return (*r)->name == data;
  683. else
  684. return 1;
  685. }
  686. /**
  687. * dev_get_regmap(): Obtain the regmap (if any) for a device
  688. *
  689. * @dev: Device to retrieve the map for
  690. * @name: Optional name for the register map, usually NULL.
  691. *
  692. * Returns the regmap for the device if one is present, or NULL. If
  693. * name is specified then it must match the name specified when
  694. * registering the device, if it is NULL then the first regmap found
  695. * will be used. Devices with multiple register maps are very rare,
  696. * generic code should normally not need to specify a name.
  697. */
  698. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  699. {
  700. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  701. dev_get_regmap_match, (void *)name);
  702. if (!r)
  703. return NULL;
  704. return *r;
  705. }
  706. EXPORT_SYMBOL_GPL(dev_get_regmap);
  707. static int _regmap_select_page(struct regmap *map, unsigned int *reg,
  708. struct regmap_range_node *range,
  709. unsigned int val_num)
  710. {
  711. void *orig_work_buf;
  712. unsigned int win_offset;
  713. unsigned int win_page;
  714. bool page_chg;
  715. int ret;
  716. win_offset = (*reg - range->range_min) % range->window_len;
  717. win_page = (*reg - range->range_min) / range->window_len;
  718. if (val_num > 1) {
  719. /* Bulk write shouldn't cross range boundary */
  720. if (*reg + val_num - 1 > range->range_max)
  721. return -EINVAL;
  722. /* ... or single page boundary */
  723. if (val_num > range->window_len - win_offset)
  724. return -EINVAL;
  725. }
  726. /* It is possible to have selector register inside data window.
  727. In that case, selector register is located on every page and
  728. it needs no page switching, when accessed alone. */
  729. if (val_num > 1 ||
  730. range->window_start + win_offset != range->selector_reg) {
  731. /* Use separate work_buf during page switching */
  732. orig_work_buf = map->work_buf;
  733. map->work_buf = map->selector_work_buf;
  734. ret = _regmap_update_bits(map, range->selector_reg,
  735. range->selector_mask,
  736. win_page << range->selector_shift,
  737. &page_chg);
  738. map->work_buf = orig_work_buf;
  739. if (ret != 0)
  740. return ret;
  741. }
  742. *reg = range->window_start + win_offset;
  743. return 0;
  744. }
  745. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  746. const void *val, size_t val_len)
  747. {
  748. struct regmap_range_node *range;
  749. u8 *u8 = map->work_buf;
  750. void *buf;
  751. int ret = -ENOTSUPP;
  752. size_t len;
  753. int i;
  754. /* Check for unwritable registers before we start */
  755. if (map->writeable_reg)
  756. for (i = 0; i < val_len / map->format.val_bytes; i++)
  757. if (!map->writeable_reg(map->dev,
  758. reg + (i * map->reg_stride)))
  759. return -EINVAL;
  760. if (!map->cache_bypass && map->format.parse_val) {
  761. unsigned int ival;
  762. int val_bytes = map->format.val_bytes;
  763. for (i = 0; i < val_len / val_bytes; i++) {
  764. memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
  765. ival = map->format.parse_val(map->work_buf);
  766. ret = regcache_write(map, reg + (i * map->reg_stride),
  767. ival);
  768. if (ret) {
  769. dev_err(map->dev,
  770. "Error in caching of register: %x ret: %d\n",
  771. reg + i, ret);
  772. return ret;
  773. }
  774. }
  775. if (map->cache_only) {
  776. map->cache_dirty = true;
  777. return 0;
  778. }
  779. }
  780. range = _regmap_range_lookup(map, reg);
  781. if (range) {
  782. int val_num = val_len / map->format.val_bytes;
  783. int win_offset = (reg - range->range_min) % range->window_len;
  784. int win_residue = range->window_len - win_offset;
  785. /* If the write goes beyond the end of the window split it */
  786. while (val_num > win_residue) {
  787. dev_dbg(map->dev, "Writing window %d/%zu\n",
  788. win_residue, val_len / map->format.val_bytes);
  789. ret = _regmap_raw_write(map, reg, val, win_residue *
  790. map->format.val_bytes);
  791. if (ret != 0)
  792. return ret;
  793. reg += win_residue;
  794. val_num -= win_residue;
  795. val += win_residue * map->format.val_bytes;
  796. val_len -= win_residue * map->format.val_bytes;
  797. win_offset = (reg - range->range_min) %
  798. range->window_len;
  799. win_residue = range->window_len - win_offset;
  800. }
  801. ret = _regmap_select_page(map, &reg, range, val_num);
  802. if (ret != 0)
  803. return ret;
  804. }
  805. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  806. u8[0] |= map->write_flag_mask;
  807. trace_regmap_hw_write_start(map->dev, reg,
  808. val_len / map->format.val_bytes);
  809. /* If we're doing a single register write we can probably just
  810. * send the work_buf directly, otherwise try to do a gather
  811. * write.
  812. */
  813. if (val == (map->work_buf + map->format.pad_bytes +
  814. map->format.reg_bytes))
  815. ret = map->bus->write(map->bus_context, map->work_buf,
  816. map->format.reg_bytes +
  817. map->format.pad_bytes +
  818. val_len);
  819. else if (map->bus->gather_write)
  820. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  821. map->format.reg_bytes +
  822. map->format.pad_bytes,
  823. val, val_len);
  824. /* If that didn't work fall back on linearising by hand. */
  825. if (ret == -ENOTSUPP) {
  826. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  827. buf = kzalloc(len, GFP_KERNEL);
  828. if (!buf)
  829. return -ENOMEM;
  830. memcpy(buf, map->work_buf, map->format.reg_bytes);
  831. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  832. val, val_len);
  833. ret = map->bus->write(map->bus_context, buf, len);
  834. kfree(buf);
  835. }
  836. trace_regmap_hw_write_done(map->dev, reg,
  837. val_len / map->format.val_bytes);
  838. return ret;
  839. }
  840. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  841. unsigned int val)
  842. {
  843. int ret;
  844. struct regmap_range_node *range;
  845. struct regmap *map = context;
  846. BUG_ON(!map->format.format_write);
  847. range = _regmap_range_lookup(map, reg);
  848. if (range) {
  849. ret = _regmap_select_page(map, &reg, range, 1);
  850. if (ret != 0)
  851. return ret;
  852. }
  853. map->format.format_write(map, reg, val);
  854. trace_regmap_hw_write_start(map->dev, reg, 1);
  855. ret = map->bus->write(map->bus_context, map->work_buf,
  856. map->format.buf_size);
  857. trace_regmap_hw_write_done(map->dev, reg, 1);
  858. return ret;
  859. }
  860. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  861. unsigned int val)
  862. {
  863. struct regmap *map = context;
  864. BUG_ON(!map->format.format_val);
  865. map->format.format_val(map->work_buf + map->format.reg_bytes
  866. + map->format.pad_bytes, val, 0);
  867. return _regmap_raw_write(map, reg,
  868. map->work_buf +
  869. map->format.reg_bytes +
  870. map->format.pad_bytes,
  871. map->format.val_bytes);
  872. }
  873. int _regmap_write(struct regmap *map, unsigned int reg,
  874. unsigned int val)
  875. {
  876. int ret;
  877. if (!map->cache_bypass && map->format.format_write) {
  878. ret = regcache_write(map, reg, val);
  879. if (ret != 0)
  880. return ret;
  881. if (map->cache_only) {
  882. map->cache_dirty = true;
  883. return 0;
  884. }
  885. }
  886. #ifdef LOG_DEVICE
  887. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  888. dev_info(map->dev, "%x <= %x\n", reg, val);
  889. #endif
  890. trace_regmap_reg_write(map->dev, reg, val);
  891. return map->reg_write(map, reg, val);
  892. }
  893. /**
  894. * regmap_write(): Write a value to a single register
  895. *
  896. * @map: Register map to write to
  897. * @reg: Register to write to
  898. * @val: Value to be written
  899. *
  900. * A value of zero will be returned on success, a negative errno will
  901. * be returned in error cases.
  902. */
  903. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  904. {
  905. int ret;
  906. if (reg % map->reg_stride)
  907. return -EINVAL;
  908. map->lock(map->lock_arg);
  909. ret = _regmap_write(map, reg, val);
  910. map->unlock(map->lock_arg);
  911. return ret;
  912. }
  913. EXPORT_SYMBOL_GPL(regmap_write);
  914. /**
  915. * regmap_raw_write(): Write raw values to one or more registers
  916. *
  917. * @map: Register map to write to
  918. * @reg: Initial register to write to
  919. * @val: Block of data to be written, laid out for direct transmission to the
  920. * device
  921. * @val_len: Length of data pointed to by val.
  922. *
  923. * This function is intended to be used for things like firmware
  924. * download where a large block of data needs to be transferred to the
  925. * device. No formatting will be done on the data provided.
  926. *
  927. * A value of zero will be returned on success, a negative errno will
  928. * be returned in error cases.
  929. */
  930. int regmap_raw_write(struct regmap *map, unsigned int reg,
  931. const void *val, size_t val_len)
  932. {
  933. int ret;
  934. if (val_len % map->format.val_bytes)
  935. return -EINVAL;
  936. if (reg % map->reg_stride)
  937. return -EINVAL;
  938. map->lock(map->lock_arg);
  939. ret = _regmap_raw_write(map, reg, val, val_len);
  940. map->unlock(map->lock_arg);
  941. return ret;
  942. }
  943. EXPORT_SYMBOL_GPL(regmap_raw_write);
  944. /*
  945. * regmap_bulk_write(): Write multiple registers to the device
  946. *
  947. * @map: Register map to write to
  948. * @reg: First register to be write from
  949. * @val: Block of data to be written, in native register size for device
  950. * @val_count: Number of registers to write
  951. *
  952. * This function is intended to be used for writing a large block of
  953. * data to be device either in single transfer or multiple transfer.
  954. *
  955. * A value of zero will be returned on success, a negative errno will
  956. * be returned in error cases.
  957. */
  958. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  959. size_t val_count)
  960. {
  961. int ret = 0, i;
  962. size_t val_bytes = map->format.val_bytes;
  963. void *wval;
  964. if (!map->format.parse_val)
  965. return -EINVAL;
  966. if (reg % map->reg_stride)
  967. return -EINVAL;
  968. map->lock(map->lock_arg);
  969. /* No formatting is require if val_byte is 1 */
  970. if (val_bytes == 1) {
  971. wval = (void *)val;
  972. } else {
  973. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  974. if (!wval) {
  975. ret = -ENOMEM;
  976. dev_err(map->dev, "Error in memory allocation\n");
  977. goto out;
  978. }
  979. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  980. map->format.parse_val(wval + i);
  981. }
  982. /*
  983. * Some devices does not support bulk write, for
  984. * them we have a series of single write operations.
  985. */
  986. if (map->use_single_rw) {
  987. for (i = 0; i < val_count; i++) {
  988. ret = regmap_raw_write(map,
  989. reg + (i * map->reg_stride),
  990. val + (i * val_bytes),
  991. val_bytes);
  992. if (ret != 0)
  993. return ret;
  994. }
  995. } else {
  996. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
  997. }
  998. if (val_bytes != 1)
  999. kfree(wval);
  1000. out:
  1001. map->unlock(map->lock_arg);
  1002. return ret;
  1003. }
  1004. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1005. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1006. unsigned int val_len)
  1007. {
  1008. struct regmap_range_node *range;
  1009. u8 *u8 = map->work_buf;
  1010. int ret;
  1011. range = _regmap_range_lookup(map, reg);
  1012. if (range) {
  1013. ret = _regmap_select_page(map, &reg, range,
  1014. val_len / map->format.val_bytes);
  1015. if (ret != 0)
  1016. return ret;
  1017. }
  1018. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1019. /*
  1020. * Some buses or devices flag reads by setting the high bits in the
  1021. * register addresss; since it's always the high bits for all
  1022. * current formats we can do this here rather than in
  1023. * formatting. This may break if we get interesting formats.
  1024. */
  1025. u8[0] |= map->read_flag_mask;
  1026. trace_regmap_hw_read_start(map->dev, reg,
  1027. val_len / map->format.val_bytes);
  1028. ret = map->bus->read(map->bus_context, map->work_buf,
  1029. map->format.reg_bytes + map->format.pad_bytes,
  1030. val, val_len);
  1031. trace_regmap_hw_read_done(map->dev, reg,
  1032. val_len / map->format.val_bytes);
  1033. return ret;
  1034. }
  1035. static int _regmap_bus_read(void *context, unsigned int reg,
  1036. unsigned int *val)
  1037. {
  1038. int ret;
  1039. struct regmap *map = context;
  1040. if (!map->format.parse_val)
  1041. return -EINVAL;
  1042. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  1043. if (ret == 0)
  1044. *val = map->format.parse_val(map->work_buf);
  1045. return ret;
  1046. }
  1047. static int _regmap_read(struct regmap *map, unsigned int reg,
  1048. unsigned int *val)
  1049. {
  1050. int ret;
  1051. BUG_ON(!map->reg_read);
  1052. if (!map->cache_bypass) {
  1053. ret = regcache_read(map, reg, val);
  1054. if (ret == 0)
  1055. return 0;
  1056. }
  1057. if (map->cache_only)
  1058. return -EBUSY;
  1059. ret = map->reg_read(map, reg, val);
  1060. if (ret == 0) {
  1061. #ifdef LOG_DEVICE
  1062. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1063. dev_info(map->dev, "%x => %x\n", reg, *val);
  1064. #endif
  1065. trace_regmap_reg_read(map->dev, reg, *val);
  1066. if (!map->cache_bypass)
  1067. regcache_write(map, reg, *val);
  1068. }
  1069. return ret;
  1070. }
  1071. /**
  1072. * regmap_read(): Read a value from a single register
  1073. *
  1074. * @map: Register map to write to
  1075. * @reg: Register to be read from
  1076. * @val: Pointer to store read value
  1077. *
  1078. * A value of zero will be returned on success, a negative errno will
  1079. * be returned in error cases.
  1080. */
  1081. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  1082. {
  1083. int ret;
  1084. if (reg % map->reg_stride)
  1085. return -EINVAL;
  1086. map->lock(map->lock_arg);
  1087. ret = _regmap_read(map, reg, val);
  1088. map->unlock(map->lock_arg);
  1089. return ret;
  1090. }
  1091. EXPORT_SYMBOL_GPL(regmap_read);
  1092. /**
  1093. * regmap_raw_read(): Read raw data from the device
  1094. *
  1095. * @map: Register map to write to
  1096. * @reg: First register to be read from
  1097. * @val: Pointer to store read value
  1098. * @val_len: Size of data to read
  1099. *
  1100. * A value of zero will be returned on success, a negative errno will
  1101. * be returned in error cases.
  1102. */
  1103. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1104. size_t val_len)
  1105. {
  1106. size_t val_bytes = map->format.val_bytes;
  1107. size_t val_count = val_len / val_bytes;
  1108. unsigned int v;
  1109. int ret, i;
  1110. if (val_len % map->format.val_bytes)
  1111. return -EINVAL;
  1112. if (reg % map->reg_stride)
  1113. return -EINVAL;
  1114. map->lock(map->lock_arg);
  1115. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  1116. map->cache_type == REGCACHE_NONE) {
  1117. /* Physical block read if there's no cache involved */
  1118. ret = _regmap_raw_read(map, reg, val, val_len);
  1119. } else {
  1120. /* Otherwise go word by word for the cache; should be low
  1121. * cost as we expect to hit the cache.
  1122. */
  1123. for (i = 0; i < val_count; i++) {
  1124. ret = _regmap_read(map, reg + (i * map->reg_stride),
  1125. &v);
  1126. if (ret != 0)
  1127. goto out;
  1128. map->format.format_val(val + (i * val_bytes), v, 0);
  1129. }
  1130. }
  1131. out:
  1132. map->unlock(map->lock_arg);
  1133. return ret;
  1134. }
  1135. EXPORT_SYMBOL_GPL(regmap_raw_read);
  1136. /**
  1137. * regmap_bulk_read(): Read multiple registers from the device
  1138. *
  1139. * @map: Register map to write to
  1140. * @reg: First register to be read from
  1141. * @val: Pointer to store read value, in native register size for device
  1142. * @val_count: Number of registers to read
  1143. *
  1144. * A value of zero will be returned on success, a negative errno will
  1145. * be returned in error cases.
  1146. */
  1147. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  1148. size_t val_count)
  1149. {
  1150. int ret, i;
  1151. size_t val_bytes = map->format.val_bytes;
  1152. bool vol = regmap_volatile_range(map, reg, val_count);
  1153. if (!map->format.parse_val)
  1154. return -EINVAL;
  1155. if (reg % map->reg_stride)
  1156. return -EINVAL;
  1157. if (vol || map->cache_type == REGCACHE_NONE) {
  1158. /*
  1159. * Some devices does not support bulk read, for
  1160. * them we have a series of single read operations.
  1161. */
  1162. if (map->use_single_rw) {
  1163. for (i = 0; i < val_count; i++) {
  1164. ret = regmap_raw_read(map,
  1165. reg + (i * map->reg_stride),
  1166. val + (i * val_bytes),
  1167. val_bytes);
  1168. if (ret != 0)
  1169. return ret;
  1170. }
  1171. } else {
  1172. ret = regmap_raw_read(map, reg, val,
  1173. val_bytes * val_count);
  1174. if (ret != 0)
  1175. return ret;
  1176. }
  1177. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1178. map->format.parse_val(val + i);
  1179. } else {
  1180. for (i = 0; i < val_count; i++) {
  1181. unsigned int ival;
  1182. ret = regmap_read(map, reg + (i * map->reg_stride),
  1183. &ival);
  1184. if (ret != 0)
  1185. return ret;
  1186. memcpy(val + (i * val_bytes), &ival, val_bytes);
  1187. }
  1188. }
  1189. return 0;
  1190. }
  1191. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  1192. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  1193. unsigned int mask, unsigned int val,
  1194. bool *change)
  1195. {
  1196. int ret;
  1197. unsigned int tmp, orig;
  1198. ret = _regmap_read(map, reg, &orig);
  1199. if (ret != 0)
  1200. return ret;
  1201. tmp = orig & ~mask;
  1202. tmp |= val & mask;
  1203. if (tmp != orig) {
  1204. ret = _regmap_write(map, reg, tmp);
  1205. *change = true;
  1206. } else {
  1207. *change = false;
  1208. }
  1209. return ret;
  1210. }
  1211. /**
  1212. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  1213. *
  1214. * @map: Register map to update
  1215. * @reg: Register to update
  1216. * @mask: Bitmask to change
  1217. * @val: New value for bitmask
  1218. *
  1219. * Returns zero for success, a negative number on error.
  1220. */
  1221. int regmap_update_bits(struct regmap *map, unsigned int reg,
  1222. unsigned int mask, unsigned int val)
  1223. {
  1224. bool change;
  1225. int ret;
  1226. map->lock(map->lock_arg);
  1227. ret = _regmap_update_bits(map, reg, mask, val, &change);
  1228. map->unlock(map->lock_arg);
  1229. return ret;
  1230. }
  1231. EXPORT_SYMBOL_GPL(regmap_update_bits);
  1232. /**
  1233. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  1234. * register map and report if updated
  1235. *
  1236. * @map: Register map to update
  1237. * @reg: Register to update
  1238. * @mask: Bitmask to change
  1239. * @val: New value for bitmask
  1240. * @change: Boolean indicating if a write was done
  1241. *
  1242. * Returns zero for success, a negative number on error.
  1243. */
  1244. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  1245. unsigned int mask, unsigned int val,
  1246. bool *change)
  1247. {
  1248. int ret;
  1249. map->lock(map->lock_arg);
  1250. ret = _regmap_update_bits(map, reg, mask, val, change);
  1251. map->unlock(map->lock_arg);
  1252. return ret;
  1253. }
  1254. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  1255. /**
  1256. * regmap_register_patch: Register and apply register updates to be applied
  1257. * on device initialistion
  1258. *
  1259. * @map: Register map to apply updates to.
  1260. * @regs: Values to update.
  1261. * @num_regs: Number of entries in regs.
  1262. *
  1263. * Register a set of register updates to be applied to the device
  1264. * whenever the device registers are synchronised with the cache and
  1265. * apply them immediately. Typically this is used to apply
  1266. * corrections to be applied to the device defaults on startup, such
  1267. * as the updates some vendors provide to undocumented registers.
  1268. */
  1269. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  1270. int num_regs)
  1271. {
  1272. int i, ret;
  1273. bool bypass;
  1274. /* If needed the implementation can be extended to support this */
  1275. if (map->patch)
  1276. return -EBUSY;
  1277. map->lock(map->lock_arg);
  1278. bypass = map->cache_bypass;
  1279. map->cache_bypass = true;
  1280. /* Write out first; it's useful to apply even if we fail later. */
  1281. for (i = 0; i < num_regs; i++) {
  1282. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1283. if (ret != 0) {
  1284. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  1285. regs[i].reg, regs[i].def, ret);
  1286. goto out;
  1287. }
  1288. }
  1289. map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
  1290. if (map->patch != NULL) {
  1291. memcpy(map->patch, regs,
  1292. num_regs * sizeof(struct reg_default));
  1293. map->patch_regs = num_regs;
  1294. } else {
  1295. ret = -ENOMEM;
  1296. }
  1297. out:
  1298. map->cache_bypass = bypass;
  1299. map->unlock(map->lock_arg);
  1300. return ret;
  1301. }
  1302. EXPORT_SYMBOL_GPL(regmap_register_patch);
  1303. /*
  1304. * regmap_get_val_bytes(): Report the size of a register value
  1305. *
  1306. * Report the size of a register value, mainly intended to for use by
  1307. * generic infrastructure built on top of regmap.
  1308. */
  1309. int regmap_get_val_bytes(struct regmap *map)
  1310. {
  1311. if (map->format.format_write)
  1312. return -EINVAL;
  1313. return map->format.val_bytes;
  1314. }
  1315. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  1316. static int __init regmap_initcall(void)
  1317. {
  1318. regmap_debugfs_initcall();
  1319. return 0;
  1320. }
  1321. postcore_initcall(regmap_initcall);