regmap.c 41 KB

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