regmap.c 43 KB

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