regmap.c 43 KB

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