regmap.c 43 KB

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