regmap.c 41 KB

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