regmap.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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. ival = map->format.parse_val(val + (i * val_bytes));
  797. ret = regcache_write(map, reg + (i * map->reg_stride),
  798. ival);
  799. if (ret) {
  800. dev_err(map->dev,
  801. "Error in caching of register: %x ret: %d\n",
  802. reg + i, ret);
  803. return ret;
  804. }
  805. }
  806. if (map->cache_only) {
  807. map->cache_dirty = true;
  808. return 0;
  809. }
  810. }
  811. range = _regmap_range_lookup(map, reg);
  812. if (range) {
  813. int val_num = val_len / map->format.val_bytes;
  814. int win_offset = (reg - range->range_min) % range->window_len;
  815. int win_residue = range->window_len - win_offset;
  816. /* If the write goes beyond the end of the window split it */
  817. while (val_num > win_residue) {
  818. dev_dbg(map->dev, "Writing window %d/%zu\n",
  819. win_residue, val_len / map->format.val_bytes);
  820. ret = _regmap_raw_write(map, reg, val, win_residue *
  821. map->format.val_bytes, async);
  822. if (ret != 0)
  823. return ret;
  824. reg += win_residue;
  825. val_num -= win_residue;
  826. val += win_residue * map->format.val_bytes;
  827. val_len -= win_residue * map->format.val_bytes;
  828. win_offset = (reg - range->range_min) %
  829. range->window_len;
  830. win_residue = range->window_len - win_offset;
  831. }
  832. ret = _regmap_select_page(map, &reg, range, val_num);
  833. if (ret != 0)
  834. return ret;
  835. }
  836. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  837. u8[0] |= map->write_flag_mask;
  838. if (async && map->bus->async_write) {
  839. struct regmap_async *async = map->bus->async_alloc();
  840. if (!async)
  841. return -ENOMEM;
  842. async->work_buf = kzalloc(map->format.buf_size,
  843. GFP_KERNEL | GFP_DMA);
  844. if (!async->work_buf) {
  845. kfree(async);
  846. return -ENOMEM;
  847. }
  848. INIT_WORK(&async->cleanup, async_cleanup);
  849. async->map = map;
  850. /* If the caller supplied the value we can use it safely. */
  851. memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
  852. map->format.reg_bytes + map->format.val_bytes);
  853. if (val == work_val)
  854. val = async->work_buf + map->format.pad_bytes +
  855. map->format.reg_bytes;
  856. spin_lock_irqsave(&map->async_lock, flags);
  857. list_add_tail(&async->list, &map->async_list);
  858. spin_unlock_irqrestore(&map->async_lock, flags);
  859. ret = map->bus->async_write(map->bus_context, async->work_buf,
  860. map->format.reg_bytes +
  861. map->format.pad_bytes,
  862. val, val_len, async);
  863. if (ret != 0) {
  864. dev_err(map->dev, "Failed to schedule write: %d\n",
  865. ret);
  866. spin_lock_irqsave(&map->async_lock, flags);
  867. list_del(&async->list);
  868. spin_unlock_irqrestore(&map->async_lock, flags);
  869. kfree(async->work_buf);
  870. kfree(async);
  871. }
  872. }
  873. trace_regmap_hw_write_start(map->dev, reg,
  874. val_len / map->format.val_bytes);
  875. /* If we're doing a single register write we can probably just
  876. * send the work_buf directly, otherwise try to do a gather
  877. * write.
  878. */
  879. if (val == work_val)
  880. ret = map->bus->write(map->bus_context, map->work_buf,
  881. map->format.reg_bytes +
  882. map->format.pad_bytes +
  883. val_len);
  884. else if (map->bus->gather_write)
  885. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  886. map->format.reg_bytes +
  887. map->format.pad_bytes,
  888. val, val_len);
  889. /* If that didn't work fall back on linearising by hand. */
  890. if (ret == -ENOTSUPP) {
  891. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  892. buf = kzalloc(len, GFP_KERNEL);
  893. if (!buf)
  894. return -ENOMEM;
  895. memcpy(buf, map->work_buf, map->format.reg_bytes);
  896. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  897. val, val_len);
  898. ret = map->bus->write(map->bus_context, buf, len);
  899. kfree(buf);
  900. }
  901. trace_regmap_hw_write_done(map->dev, reg,
  902. val_len / map->format.val_bytes);
  903. return ret;
  904. }
  905. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  906. unsigned int val)
  907. {
  908. int ret;
  909. struct regmap_range_node *range;
  910. struct regmap *map = context;
  911. BUG_ON(!map->bus || !map->format.format_write);
  912. range = _regmap_range_lookup(map, reg);
  913. if (range) {
  914. ret = _regmap_select_page(map, &reg, range, 1);
  915. if (ret != 0)
  916. return ret;
  917. }
  918. map->format.format_write(map, reg, val);
  919. trace_regmap_hw_write_start(map->dev, reg, 1);
  920. ret = map->bus->write(map->bus_context, map->work_buf,
  921. map->format.buf_size);
  922. trace_regmap_hw_write_done(map->dev, reg, 1);
  923. return ret;
  924. }
  925. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  926. unsigned int val)
  927. {
  928. struct regmap *map = context;
  929. BUG_ON(!map->bus || !map->format.format_val);
  930. map->format.format_val(map->work_buf + map->format.reg_bytes
  931. + map->format.pad_bytes, val, 0);
  932. return _regmap_raw_write(map, reg,
  933. map->work_buf +
  934. map->format.reg_bytes +
  935. map->format.pad_bytes,
  936. map->format.val_bytes, false);
  937. }
  938. static inline void *_regmap_map_get_context(struct regmap *map)
  939. {
  940. return (map->bus) ? map : map->bus_context;
  941. }
  942. int _regmap_write(struct regmap *map, unsigned int reg,
  943. unsigned int val)
  944. {
  945. int ret;
  946. void *context = _regmap_map_get_context(map);
  947. if (!map->cache_bypass && !map->defer_caching) {
  948. ret = regcache_write(map, reg, val);
  949. if (ret != 0)
  950. return ret;
  951. if (map->cache_only) {
  952. map->cache_dirty = true;
  953. return 0;
  954. }
  955. }
  956. #ifdef LOG_DEVICE
  957. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  958. dev_info(map->dev, "%x <= %x\n", reg, val);
  959. #endif
  960. trace_regmap_reg_write(map->dev, reg, val);
  961. return map->reg_write(context, reg, val);
  962. }
  963. /**
  964. * regmap_write(): Write a value to a single register
  965. *
  966. * @map: Register map to write to
  967. * @reg: Register to write to
  968. * @val: Value to be written
  969. *
  970. * A value of zero will be returned on success, a negative errno will
  971. * be returned in error cases.
  972. */
  973. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  974. {
  975. int ret;
  976. if (reg % map->reg_stride)
  977. return -EINVAL;
  978. map->lock(map->lock_arg);
  979. ret = _regmap_write(map, reg, val);
  980. map->unlock(map->lock_arg);
  981. return ret;
  982. }
  983. EXPORT_SYMBOL_GPL(regmap_write);
  984. /**
  985. * regmap_raw_write(): Write raw values to one or more registers
  986. *
  987. * @map: Register map to write to
  988. * @reg: Initial register to write to
  989. * @val: Block of data to be written, laid out for direct transmission to the
  990. * device
  991. * @val_len: Length of data pointed to by val.
  992. *
  993. * This function is intended to be used for things like firmware
  994. * download where a large block of data needs to be transferred to the
  995. * device. No formatting will be done on the data provided.
  996. *
  997. * A value of zero will be returned on success, a negative errno will
  998. * be returned in error cases.
  999. */
  1000. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1001. const void *val, size_t val_len)
  1002. {
  1003. int ret;
  1004. if (!map->bus)
  1005. return -EINVAL;
  1006. if (val_len % map->format.val_bytes)
  1007. return -EINVAL;
  1008. if (reg % map->reg_stride)
  1009. return -EINVAL;
  1010. map->lock(map->lock_arg);
  1011. ret = _regmap_raw_write(map, reg, val, val_len, false);
  1012. map->unlock(map->lock_arg);
  1013. return ret;
  1014. }
  1015. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1016. /*
  1017. * regmap_bulk_write(): Write multiple registers to the device
  1018. *
  1019. * @map: Register map to write to
  1020. * @reg: First register to be write from
  1021. * @val: Block of data to be written, in native register size for device
  1022. * @val_count: Number of registers to write
  1023. *
  1024. * This function is intended to be used for writing a large block of
  1025. * data to the device either in single transfer or multiple transfer.
  1026. *
  1027. * A value of zero will be returned on success, a negative errno will
  1028. * be returned in error cases.
  1029. */
  1030. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1031. size_t val_count)
  1032. {
  1033. int ret = 0, i;
  1034. size_t val_bytes = map->format.val_bytes;
  1035. void *wval;
  1036. if (!map->bus)
  1037. return -EINVAL;
  1038. if (!map->format.parse_val)
  1039. return -EINVAL;
  1040. if (reg % map->reg_stride)
  1041. return -EINVAL;
  1042. map->lock(map->lock_arg);
  1043. /* No formatting is require if val_byte is 1 */
  1044. if (val_bytes == 1) {
  1045. wval = (void *)val;
  1046. } else {
  1047. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  1048. if (!wval) {
  1049. ret = -ENOMEM;
  1050. dev_err(map->dev, "Error in memory allocation\n");
  1051. goto out;
  1052. }
  1053. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1054. map->format.parse_val(wval + i);
  1055. }
  1056. /*
  1057. * Some devices does not support bulk write, for
  1058. * them we have a series of single write operations.
  1059. */
  1060. if (map->use_single_rw) {
  1061. for (i = 0; i < val_count; i++) {
  1062. ret = regmap_raw_write(map,
  1063. reg + (i * map->reg_stride),
  1064. val + (i * val_bytes),
  1065. val_bytes);
  1066. if (ret != 0)
  1067. return ret;
  1068. }
  1069. } else {
  1070. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count,
  1071. false);
  1072. }
  1073. if (val_bytes != 1)
  1074. kfree(wval);
  1075. out:
  1076. map->unlock(map->lock_arg);
  1077. return ret;
  1078. }
  1079. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1080. /**
  1081. * regmap_raw_write_async(): Write raw values to one or more registers
  1082. * asynchronously
  1083. *
  1084. * @map: Register map to write to
  1085. * @reg: Initial register to write to
  1086. * @val: Block of data to be written, laid out for direct transmission to the
  1087. * device. Must be valid until regmap_async_complete() is called.
  1088. * @val_len: Length of data pointed to by val.
  1089. *
  1090. * This function is intended to be used for things like firmware
  1091. * download where a large block of data needs to be transferred to the
  1092. * device. No formatting will be done on the data provided.
  1093. *
  1094. * If supported by the underlying bus the write will be scheduled
  1095. * asynchronously, helping maximise I/O speed on higher speed buses
  1096. * like SPI. regmap_async_complete() can be called to ensure that all
  1097. * asynchrnous writes have been completed.
  1098. *
  1099. * A value of zero will be returned on success, a negative errno will
  1100. * be returned in error cases.
  1101. */
  1102. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  1103. const void *val, size_t val_len)
  1104. {
  1105. int ret;
  1106. if (val_len % map->format.val_bytes)
  1107. return -EINVAL;
  1108. if (reg % map->reg_stride)
  1109. return -EINVAL;
  1110. map->lock(map->lock_arg);
  1111. ret = _regmap_raw_write(map, reg, val, val_len, true);
  1112. map->unlock(map->lock_arg);
  1113. return ret;
  1114. }
  1115. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  1116. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1117. unsigned int val_len)
  1118. {
  1119. struct regmap_range_node *range;
  1120. u8 *u8 = map->work_buf;
  1121. int ret;
  1122. BUG_ON(!map->bus);
  1123. range = _regmap_range_lookup(map, reg);
  1124. if (range) {
  1125. ret = _regmap_select_page(map, &reg, range,
  1126. val_len / map->format.val_bytes);
  1127. if (ret != 0)
  1128. return ret;
  1129. }
  1130. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1131. /*
  1132. * Some buses or devices flag reads by setting the high bits in the
  1133. * register addresss; since it's always the high bits for all
  1134. * current formats we can do this here rather than in
  1135. * formatting. This may break if we get interesting formats.
  1136. */
  1137. u8[0] |= map->read_flag_mask;
  1138. trace_regmap_hw_read_start(map->dev, reg,
  1139. val_len / map->format.val_bytes);
  1140. ret = map->bus->read(map->bus_context, map->work_buf,
  1141. map->format.reg_bytes + map->format.pad_bytes,
  1142. val, val_len);
  1143. trace_regmap_hw_read_done(map->dev, reg,
  1144. val_len / map->format.val_bytes);
  1145. return ret;
  1146. }
  1147. static int _regmap_bus_read(void *context, unsigned int reg,
  1148. unsigned int *val)
  1149. {
  1150. int ret;
  1151. struct regmap *map = context;
  1152. if (!map->format.parse_val)
  1153. return -EINVAL;
  1154. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  1155. if (ret == 0)
  1156. *val = map->format.parse_val(map->work_buf);
  1157. return ret;
  1158. }
  1159. static int _regmap_read(struct regmap *map, unsigned int reg,
  1160. unsigned int *val)
  1161. {
  1162. int ret;
  1163. void *context = _regmap_map_get_context(map);
  1164. BUG_ON(!map->reg_read);
  1165. if (!map->cache_bypass) {
  1166. ret = regcache_read(map, reg, val);
  1167. if (ret == 0)
  1168. return 0;
  1169. }
  1170. if (map->cache_only)
  1171. return -EBUSY;
  1172. ret = map->reg_read(context, reg, val);
  1173. if (ret == 0) {
  1174. #ifdef LOG_DEVICE
  1175. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1176. dev_info(map->dev, "%x => %x\n", reg, *val);
  1177. #endif
  1178. trace_regmap_reg_read(map->dev, reg, *val);
  1179. if (!map->cache_bypass)
  1180. regcache_write(map, reg, *val);
  1181. }
  1182. return ret;
  1183. }
  1184. /**
  1185. * regmap_read(): Read a value from a single register
  1186. *
  1187. * @map: Register map to write to
  1188. * @reg: Register to be read from
  1189. * @val: Pointer to store read value
  1190. *
  1191. * A value of zero will be returned on success, a negative errno will
  1192. * be returned in error cases.
  1193. */
  1194. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  1195. {
  1196. int ret;
  1197. if (reg % map->reg_stride)
  1198. return -EINVAL;
  1199. map->lock(map->lock_arg);
  1200. ret = _regmap_read(map, reg, val);
  1201. map->unlock(map->lock_arg);
  1202. return ret;
  1203. }
  1204. EXPORT_SYMBOL_GPL(regmap_read);
  1205. /**
  1206. * regmap_raw_read(): Read raw data from the device
  1207. *
  1208. * @map: Register map to write to
  1209. * @reg: First register to be read from
  1210. * @val: Pointer to store read value
  1211. * @val_len: Size of data to read
  1212. *
  1213. * A value of zero will be returned on success, a negative errno will
  1214. * be returned in error cases.
  1215. */
  1216. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1217. size_t val_len)
  1218. {
  1219. size_t val_bytes = map->format.val_bytes;
  1220. size_t val_count = val_len / val_bytes;
  1221. unsigned int v;
  1222. int ret, i;
  1223. if (!map->bus)
  1224. return -EINVAL;
  1225. if (val_len % map->format.val_bytes)
  1226. return -EINVAL;
  1227. if (reg % map->reg_stride)
  1228. return -EINVAL;
  1229. map->lock(map->lock_arg);
  1230. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  1231. map->cache_type == REGCACHE_NONE) {
  1232. /* Physical block read if there's no cache involved */
  1233. ret = _regmap_raw_read(map, reg, val, val_len);
  1234. } else {
  1235. /* Otherwise go word by word for the cache; should be low
  1236. * cost as we expect to hit the cache.
  1237. */
  1238. for (i = 0; i < val_count; i++) {
  1239. ret = _regmap_read(map, reg + (i * map->reg_stride),
  1240. &v);
  1241. if (ret != 0)
  1242. goto out;
  1243. map->format.format_val(val + (i * val_bytes), v, 0);
  1244. }
  1245. }
  1246. out:
  1247. map->unlock(map->lock_arg);
  1248. return ret;
  1249. }
  1250. EXPORT_SYMBOL_GPL(regmap_raw_read);
  1251. /**
  1252. * regmap_bulk_read(): Read multiple registers from the device
  1253. *
  1254. * @map: Register map to write to
  1255. * @reg: First register to be read from
  1256. * @val: Pointer to store read value, in native register size for device
  1257. * @val_count: Number of registers to read
  1258. *
  1259. * A value of zero will be returned on success, a negative errno will
  1260. * be returned in error cases.
  1261. */
  1262. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  1263. size_t val_count)
  1264. {
  1265. int ret, i;
  1266. size_t val_bytes = map->format.val_bytes;
  1267. bool vol = regmap_volatile_range(map, reg, val_count);
  1268. if (!map->bus)
  1269. return -EINVAL;
  1270. if (!map->format.parse_val)
  1271. return -EINVAL;
  1272. if (reg % map->reg_stride)
  1273. return -EINVAL;
  1274. if (vol || map->cache_type == REGCACHE_NONE) {
  1275. /*
  1276. * Some devices does not support bulk read, for
  1277. * them we have a series of single read operations.
  1278. */
  1279. if (map->use_single_rw) {
  1280. for (i = 0; i < val_count; i++) {
  1281. ret = regmap_raw_read(map,
  1282. reg + (i * map->reg_stride),
  1283. val + (i * val_bytes),
  1284. val_bytes);
  1285. if (ret != 0)
  1286. return ret;
  1287. }
  1288. } else {
  1289. ret = regmap_raw_read(map, reg, val,
  1290. val_bytes * val_count);
  1291. if (ret != 0)
  1292. return ret;
  1293. }
  1294. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1295. map->format.parse_val(val + i);
  1296. } else {
  1297. for (i = 0; i < val_count; i++) {
  1298. unsigned int ival;
  1299. ret = regmap_read(map, reg + (i * map->reg_stride),
  1300. &ival);
  1301. if (ret != 0)
  1302. return ret;
  1303. memcpy(val + (i * val_bytes), &ival, val_bytes);
  1304. }
  1305. }
  1306. return 0;
  1307. }
  1308. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  1309. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  1310. unsigned int mask, unsigned int val,
  1311. bool *change)
  1312. {
  1313. int ret;
  1314. unsigned int tmp, orig;
  1315. ret = _regmap_read(map, reg, &orig);
  1316. if (ret != 0)
  1317. return ret;
  1318. tmp = orig & ~mask;
  1319. tmp |= val & mask;
  1320. if (tmp != orig) {
  1321. ret = _regmap_write(map, reg, tmp);
  1322. *change = true;
  1323. } else {
  1324. *change = false;
  1325. }
  1326. return ret;
  1327. }
  1328. /**
  1329. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  1330. *
  1331. * @map: Register map to update
  1332. * @reg: Register to update
  1333. * @mask: Bitmask to change
  1334. * @val: New value for bitmask
  1335. *
  1336. * Returns zero for success, a negative number on error.
  1337. */
  1338. int regmap_update_bits(struct regmap *map, unsigned int reg,
  1339. unsigned int mask, unsigned int val)
  1340. {
  1341. bool change;
  1342. int ret;
  1343. map->lock(map->lock_arg);
  1344. ret = _regmap_update_bits(map, reg, mask, val, &change);
  1345. map->unlock(map->lock_arg);
  1346. return ret;
  1347. }
  1348. EXPORT_SYMBOL_GPL(regmap_update_bits);
  1349. /**
  1350. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  1351. * register map and report if updated
  1352. *
  1353. * @map: Register map to update
  1354. * @reg: Register to update
  1355. * @mask: Bitmask to change
  1356. * @val: New value for bitmask
  1357. * @change: Boolean indicating if a write was done
  1358. *
  1359. * Returns zero for success, a negative number on error.
  1360. */
  1361. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  1362. unsigned int mask, unsigned int val,
  1363. bool *change)
  1364. {
  1365. int ret;
  1366. map->lock(map->lock_arg);
  1367. ret = _regmap_update_bits(map, reg, mask, val, change);
  1368. map->unlock(map->lock_arg);
  1369. return ret;
  1370. }
  1371. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  1372. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  1373. {
  1374. struct regmap *map = async->map;
  1375. bool wake;
  1376. spin_lock(&map->async_lock);
  1377. list_del(&async->list);
  1378. wake = list_empty(&map->async_list);
  1379. if (ret != 0)
  1380. map->async_ret = ret;
  1381. spin_unlock(&map->async_lock);
  1382. schedule_work(&async->cleanup);
  1383. if (wake)
  1384. wake_up(&map->async_waitq);
  1385. }
  1386. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  1387. static int regmap_async_is_done(struct regmap *map)
  1388. {
  1389. unsigned long flags;
  1390. int ret;
  1391. spin_lock_irqsave(&map->async_lock, flags);
  1392. ret = list_empty(&map->async_list);
  1393. spin_unlock_irqrestore(&map->async_lock, flags);
  1394. return ret;
  1395. }
  1396. /**
  1397. * regmap_async_complete: Ensure all asynchronous I/O has completed.
  1398. *
  1399. * @map: Map to operate on.
  1400. *
  1401. * Blocks until any pending asynchronous I/O has completed. Returns
  1402. * an error code for any failed I/O operations.
  1403. */
  1404. int regmap_async_complete(struct regmap *map)
  1405. {
  1406. unsigned long flags;
  1407. int ret;
  1408. /* Nothing to do with no async support */
  1409. if (!map->bus->async_write)
  1410. return 0;
  1411. wait_event(map->async_waitq, regmap_async_is_done(map));
  1412. spin_lock_irqsave(&map->async_lock, flags);
  1413. ret = map->async_ret;
  1414. map->async_ret = 0;
  1415. spin_unlock_irqrestore(&map->async_lock, flags);
  1416. return ret;
  1417. }
  1418. EXPORT_SYMBOL_GPL(regmap_async_complete);
  1419. /**
  1420. * regmap_register_patch: Register and apply register updates to be applied
  1421. * on device initialistion
  1422. *
  1423. * @map: Register map to apply updates to.
  1424. * @regs: Values to update.
  1425. * @num_regs: Number of entries in regs.
  1426. *
  1427. * Register a set of register updates to be applied to the device
  1428. * whenever the device registers are synchronised with the cache and
  1429. * apply them immediately. Typically this is used to apply
  1430. * corrections to be applied to the device defaults on startup, such
  1431. * as the updates some vendors provide to undocumented registers.
  1432. */
  1433. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  1434. int num_regs)
  1435. {
  1436. int i, ret;
  1437. bool bypass;
  1438. /* If needed the implementation can be extended to support this */
  1439. if (map->patch)
  1440. return -EBUSY;
  1441. map->lock(map->lock_arg);
  1442. bypass = map->cache_bypass;
  1443. map->cache_bypass = true;
  1444. /* Write out first; it's useful to apply even if we fail later. */
  1445. for (i = 0; i < num_regs; i++) {
  1446. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1447. if (ret != 0) {
  1448. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  1449. regs[i].reg, regs[i].def, ret);
  1450. goto out;
  1451. }
  1452. }
  1453. map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
  1454. if (map->patch != NULL) {
  1455. memcpy(map->patch, regs,
  1456. num_regs * sizeof(struct reg_default));
  1457. map->patch_regs = num_regs;
  1458. } else {
  1459. ret = -ENOMEM;
  1460. }
  1461. out:
  1462. map->cache_bypass = bypass;
  1463. map->unlock(map->lock_arg);
  1464. return ret;
  1465. }
  1466. EXPORT_SYMBOL_GPL(regmap_register_patch);
  1467. /*
  1468. * regmap_get_val_bytes(): Report the size of a register value
  1469. *
  1470. * Report the size of a register value, mainly intended to for use by
  1471. * generic infrastructure built on top of regmap.
  1472. */
  1473. int regmap_get_val_bytes(struct regmap *map)
  1474. {
  1475. if (map->format.format_write)
  1476. return -EINVAL;
  1477. return map->format.val_bytes;
  1478. }
  1479. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  1480. static int __init regmap_initcall(void)
  1481. {
  1482. regmap_debugfs_initcall();
  1483. return 0;
  1484. }
  1485. postcore_initcall(regmap_initcall);