regmap.c 42 KB

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