regmap.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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. #define CREATE_TRACE_POINTS
  18. #include <trace/events/regmap.h>
  19. #include "internal.h"
  20. /*
  21. * Sometimes for failures during very early init the trace
  22. * infrastructure isn't available early enough to be used. For this
  23. * sort of problem defining LOG_DEVICE will add printks for basic
  24. * register I/O on a specific device.
  25. */
  26. #undef LOG_DEVICE
  27. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  28. unsigned int mask, unsigned int val,
  29. bool *change);
  30. bool regmap_writeable(struct regmap *map, unsigned int reg)
  31. {
  32. if (map->max_register && reg > map->max_register)
  33. return false;
  34. if (map->writeable_reg)
  35. return map->writeable_reg(map->dev, reg);
  36. return true;
  37. }
  38. bool regmap_readable(struct regmap *map, unsigned int reg)
  39. {
  40. if (map->max_register && reg > map->max_register)
  41. return false;
  42. if (map->format.format_write)
  43. return false;
  44. if (map->readable_reg)
  45. return map->readable_reg(map->dev, reg);
  46. return true;
  47. }
  48. bool regmap_volatile(struct regmap *map, unsigned int reg)
  49. {
  50. if (!regmap_readable(map, reg))
  51. return false;
  52. if (map->volatile_reg)
  53. return map->volatile_reg(map->dev, reg);
  54. return true;
  55. }
  56. bool regmap_precious(struct regmap *map, unsigned int reg)
  57. {
  58. if (!regmap_readable(map, reg))
  59. return false;
  60. if (map->precious_reg)
  61. return map->precious_reg(map->dev, reg);
  62. return false;
  63. }
  64. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  65. unsigned int num)
  66. {
  67. unsigned int i;
  68. for (i = 0; i < num; i++)
  69. if (!regmap_volatile(map, reg + i))
  70. return false;
  71. return true;
  72. }
  73. static void regmap_format_2_6_write(struct regmap *map,
  74. unsigned int reg, unsigned int val)
  75. {
  76. u8 *out = map->work_buf;
  77. *out = (reg << 6) | val;
  78. }
  79. static void regmap_format_4_12_write(struct regmap *map,
  80. unsigned int reg, unsigned int val)
  81. {
  82. __be16 *out = map->work_buf;
  83. *out = cpu_to_be16((reg << 12) | val);
  84. }
  85. static void regmap_format_7_9_write(struct regmap *map,
  86. unsigned int reg, unsigned int val)
  87. {
  88. __be16 *out = map->work_buf;
  89. *out = cpu_to_be16((reg << 9) | val);
  90. }
  91. static void regmap_format_10_14_write(struct regmap *map,
  92. unsigned int reg, unsigned int val)
  93. {
  94. u8 *out = map->work_buf;
  95. out[2] = val;
  96. out[1] = (val >> 8) | (reg << 6);
  97. out[0] = reg >> 2;
  98. }
  99. static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
  100. {
  101. u8 *b = buf;
  102. b[0] = val << shift;
  103. }
  104. static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
  105. {
  106. __be16 *b = buf;
  107. b[0] = cpu_to_be16(val << shift);
  108. }
  109. static void regmap_format_16_native(void *buf, unsigned int val,
  110. unsigned int shift)
  111. {
  112. *(u16 *)buf = val << shift;
  113. }
  114. static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
  115. {
  116. u8 *b = buf;
  117. val <<= shift;
  118. b[0] = val >> 16;
  119. b[1] = val >> 8;
  120. b[2] = val;
  121. }
  122. static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
  123. {
  124. __be32 *b = buf;
  125. b[0] = cpu_to_be32(val << shift);
  126. }
  127. static void regmap_format_32_native(void *buf, unsigned int val,
  128. unsigned int shift)
  129. {
  130. *(u32 *)buf = val << shift;
  131. }
  132. static unsigned int regmap_parse_8(void *buf)
  133. {
  134. u8 *b = buf;
  135. return b[0];
  136. }
  137. static unsigned int regmap_parse_16_be(void *buf)
  138. {
  139. __be16 *b = buf;
  140. b[0] = be16_to_cpu(b[0]);
  141. return b[0];
  142. }
  143. static unsigned int regmap_parse_16_native(void *buf)
  144. {
  145. return *(u16 *)buf;
  146. }
  147. static unsigned int regmap_parse_24(void *buf)
  148. {
  149. u8 *b = buf;
  150. unsigned int ret = b[2];
  151. ret |= ((unsigned int)b[1]) << 8;
  152. ret |= ((unsigned int)b[0]) << 16;
  153. return ret;
  154. }
  155. static unsigned int regmap_parse_32_be(void *buf)
  156. {
  157. __be32 *b = buf;
  158. b[0] = be32_to_cpu(b[0]);
  159. return b[0];
  160. }
  161. static unsigned int regmap_parse_32_native(void *buf)
  162. {
  163. return *(u32 *)buf;
  164. }
  165. static void regmap_lock_mutex(struct regmap *map)
  166. {
  167. mutex_lock(&map->mutex);
  168. }
  169. static void regmap_unlock_mutex(struct regmap *map)
  170. {
  171. mutex_unlock(&map->mutex);
  172. }
  173. static void regmap_lock_spinlock(struct regmap *map)
  174. {
  175. spin_lock(&map->spinlock);
  176. }
  177. static void regmap_unlock_spinlock(struct regmap *map)
  178. {
  179. spin_unlock(&map->spinlock);
  180. }
  181. static void dev_get_regmap_release(struct device *dev, void *res)
  182. {
  183. /*
  184. * We don't actually have anything to do here; the goal here
  185. * is not to manage the regmap but to provide a simple way to
  186. * get the regmap back given a struct device.
  187. */
  188. }
  189. /**
  190. * regmap_init(): Initialise register map
  191. *
  192. * @dev: Device that will be interacted with
  193. * @bus: Bus-specific callbacks to use with device
  194. * @bus_context: Data passed to bus-specific callbacks
  195. * @config: Configuration for register map
  196. *
  197. * The return value will be an ERR_PTR() on error or a valid pointer to
  198. * a struct regmap. This function should generally not be called
  199. * directly, it should be called by bus-specific init functions.
  200. */
  201. struct regmap *regmap_init(struct device *dev,
  202. const struct regmap_bus *bus,
  203. void *bus_context,
  204. const struct regmap_config *config)
  205. {
  206. struct regmap *map, **m;
  207. int ret = -EINVAL;
  208. enum regmap_endian reg_endian, val_endian;
  209. if (!bus || !config)
  210. goto err;
  211. map = kzalloc(sizeof(*map), GFP_KERNEL);
  212. if (map == NULL) {
  213. ret = -ENOMEM;
  214. goto err;
  215. }
  216. if (bus->fast_io) {
  217. spin_lock_init(&map->spinlock);
  218. map->lock = regmap_lock_spinlock;
  219. map->unlock = regmap_unlock_spinlock;
  220. } else {
  221. mutex_init(&map->mutex);
  222. map->lock = regmap_lock_mutex;
  223. map->unlock = regmap_unlock_mutex;
  224. }
  225. map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
  226. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  227. map->format.pad_bytes = config->pad_bits / 8;
  228. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  229. map->format.buf_size += map->format.pad_bytes;
  230. map->reg_shift = config->pad_bits % 8;
  231. if (config->reg_stride)
  232. map->reg_stride = config->reg_stride;
  233. else
  234. map->reg_stride = 1;
  235. map->use_single_rw = config->use_single_rw;
  236. map->dev = dev;
  237. map->bus = bus;
  238. map->bus_context = bus_context;
  239. map->max_register = config->max_register;
  240. map->writeable_reg = config->writeable_reg;
  241. map->readable_reg = config->readable_reg;
  242. map->volatile_reg = config->volatile_reg;
  243. map->precious_reg = config->precious_reg;
  244. map->cache_type = config->cache_type;
  245. map->name = config->name;
  246. if (config->read_flag_mask || config->write_flag_mask) {
  247. map->read_flag_mask = config->read_flag_mask;
  248. map->write_flag_mask = config->write_flag_mask;
  249. } else {
  250. map->read_flag_mask = bus->read_flag_mask;
  251. }
  252. reg_endian = config->reg_format_endian;
  253. if (reg_endian == REGMAP_ENDIAN_DEFAULT)
  254. reg_endian = bus->reg_format_endian_default;
  255. if (reg_endian == REGMAP_ENDIAN_DEFAULT)
  256. reg_endian = REGMAP_ENDIAN_BIG;
  257. val_endian = config->val_format_endian;
  258. if (val_endian == REGMAP_ENDIAN_DEFAULT)
  259. val_endian = bus->val_format_endian_default;
  260. if (val_endian == REGMAP_ENDIAN_DEFAULT)
  261. val_endian = REGMAP_ENDIAN_BIG;
  262. switch (config->reg_bits + map->reg_shift) {
  263. case 2:
  264. switch (config->val_bits) {
  265. case 6:
  266. map->format.format_write = regmap_format_2_6_write;
  267. break;
  268. default:
  269. goto err_map;
  270. }
  271. break;
  272. case 4:
  273. switch (config->val_bits) {
  274. case 12:
  275. map->format.format_write = regmap_format_4_12_write;
  276. break;
  277. default:
  278. goto err_map;
  279. }
  280. break;
  281. case 7:
  282. switch (config->val_bits) {
  283. case 9:
  284. map->format.format_write = regmap_format_7_9_write;
  285. break;
  286. default:
  287. goto err_map;
  288. }
  289. break;
  290. case 10:
  291. switch (config->val_bits) {
  292. case 14:
  293. map->format.format_write = regmap_format_10_14_write;
  294. break;
  295. default:
  296. goto err_map;
  297. }
  298. break;
  299. case 8:
  300. map->format.format_reg = regmap_format_8;
  301. break;
  302. case 16:
  303. switch (reg_endian) {
  304. case REGMAP_ENDIAN_BIG:
  305. map->format.format_reg = regmap_format_16_be;
  306. break;
  307. case REGMAP_ENDIAN_NATIVE:
  308. map->format.format_reg = regmap_format_16_native;
  309. break;
  310. default:
  311. goto err_map;
  312. }
  313. break;
  314. case 32:
  315. switch (reg_endian) {
  316. case REGMAP_ENDIAN_BIG:
  317. map->format.format_reg = regmap_format_32_be;
  318. break;
  319. case REGMAP_ENDIAN_NATIVE:
  320. map->format.format_reg = regmap_format_32_native;
  321. break;
  322. default:
  323. goto err_map;
  324. }
  325. break;
  326. default:
  327. goto err_map;
  328. }
  329. switch (config->val_bits) {
  330. case 8:
  331. map->format.format_val = regmap_format_8;
  332. map->format.parse_val = regmap_parse_8;
  333. break;
  334. case 16:
  335. switch (val_endian) {
  336. case REGMAP_ENDIAN_BIG:
  337. map->format.format_val = regmap_format_16_be;
  338. map->format.parse_val = regmap_parse_16_be;
  339. break;
  340. case REGMAP_ENDIAN_NATIVE:
  341. map->format.format_val = regmap_format_16_native;
  342. map->format.parse_val = regmap_parse_16_native;
  343. break;
  344. default:
  345. goto err_map;
  346. }
  347. break;
  348. case 24:
  349. if (val_endian != REGMAP_ENDIAN_BIG)
  350. goto err_map;
  351. map->format.format_val = regmap_format_24;
  352. map->format.parse_val = regmap_parse_24;
  353. break;
  354. case 32:
  355. switch (val_endian) {
  356. case REGMAP_ENDIAN_BIG:
  357. map->format.format_val = regmap_format_32_be;
  358. map->format.parse_val = regmap_parse_32_be;
  359. break;
  360. case REGMAP_ENDIAN_NATIVE:
  361. map->format.format_val = regmap_format_32_native;
  362. map->format.parse_val = regmap_parse_32_native;
  363. break;
  364. default:
  365. goto err_map;
  366. }
  367. break;
  368. }
  369. if (map->format.format_write) {
  370. if ((reg_endian != REGMAP_ENDIAN_BIG) ||
  371. (val_endian != REGMAP_ENDIAN_BIG))
  372. goto err_map;
  373. map->use_single_rw = true;
  374. }
  375. if (!map->format.format_write &&
  376. !(map->format.format_reg && map->format.format_val))
  377. goto err_map;
  378. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  379. if (map->work_buf == NULL) {
  380. ret = -ENOMEM;
  381. goto err_map;
  382. }
  383. regmap_debugfs_init(map, config->name);
  384. ret = regcache_init(map, config);
  385. if (ret < 0)
  386. goto err_free_workbuf;
  387. /* Add a devres resource for dev_get_regmap() */
  388. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  389. if (!m) {
  390. ret = -ENOMEM;
  391. goto err_cache;
  392. }
  393. *m = map;
  394. devres_add(dev, m);
  395. return map;
  396. err_cache:
  397. regcache_exit(map);
  398. err_free_workbuf:
  399. kfree(map->work_buf);
  400. err_map:
  401. kfree(map);
  402. err:
  403. return ERR_PTR(ret);
  404. }
  405. EXPORT_SYMBOL_GPL(regmap_init);
  406. static void devm_regmap_release(struct device *dev, void *res)
  407. {
  408. regmap_exit(*(struct regmap **)res);
  409. }
  410. /**
  411. * devm_regmap_init(): Initialise managed register map
  412. *
  413. * @dev: Device that will be interacted with
  414. * @bus: Bus-specific callbacks to use with device
  415. * @bus_context: Data passed to bus-specific callbacks
  416. * @config: Configuration for register map
  417. *
  418. * The return value will be an ERR_PTR() on error or a valid pointer
  419. * to a struct regmap. This function should generally not be called
  420. * directly, it should be called by bus-specific init functions. The
  421. * map will be automatically freed by the device management code.
  422. */
  423. struct regmap *devm_regmap_init(struct device *dev,
  424. const struct regmap_bus *bus,
  425. void *bus_context,
  426. const struct regmap_config *config)
  427. {
  428. struct regmap **ptr, *regmap;
  429. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  430. if (!ptr)
  431. return ERR_PTR(-ENOMEM);
  432. regmap = regmap_init(dev, bus, bus_context, config);
  433. if (!IS_ERR(regmap)) {
  434. *ptr = regmap;
  435. devres_add(dev, ptr);
  436. } else {
  437. devres_free(ptr);
  438. }
  439. return regmap;
  440. }
  441. EXPORT_SYMBOL_GPL(devm_regmap_init);
  442. /**
  443. * regmap_reinit_cache(): Reinitialise the current register cache
  444. *
  445. * @map: Register map to operate on.
  446. * @config: New configuration. Only the cache data will be used.
  447. *
  448. * Discard any existing register cache for the map and initialize a
  449. * new cache. This can be used to restore the cache to defaults or to
  450. * update the cache configuration to reflect runtime discovery of the
  451. * hardware.
  452. */
  453. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  454. {
  455. int ret;
  456. map->lock(map);
  457. regcache_exit(map);
  458. regmap_debugfs_exit(map);
  459. map->max_register = config->max_register;
  460. map->writeable_reg = config->writeable_reg;
  461. map->readable_reg = config->readable_reg;
  462. map->volatile_reg = config->volatile_reg;
  463. map->precious_reg = config->precious_reg;
  464. map->cache_type = config->cache_type;
  465. regmap_debugfs_init(map, config->name);
  466. map->cache_bypass = false;
  467. map->cache_only = false;
  468. ret = regcache_init(map, config);
  469. map->unlock(map);
  470. return ret;
  471. }
  472. /**
  473. * regmap_exit(): Free a previously allocated register map
  474. */
  475. void regmap_exit(struct regmap *map)
  476. {
  477. regcache_exit(map);
  478. regmap_debugfs_exit(map);
  479. if (map->bus->free_context)
  480. map->bus->free_context(map->bus_context);
  481. kfree(map->work_buf);
  482. kfree(map);
  483. }
  484. EXPORT_SYMBOL_GPL(regmap_exit);
  485. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  486. {
  487. struct regmap **r = res;
  488. if (!r || !*r) {
  489. WARN_ON(!r || !*r);
  490. return 0;
  491. }
  492. /* If the user didn't specify a name match any */
  493. if (data)
  494. return (*r)->name == data;
  495. else
  496. return 1;
  497. }
  498. /**
  499. * dev_get_regmap(): Obtain the regmap (if any) for a device
  500. *
  501. * @dev: Device to retrieve the map for
  502. * @name: Optional name for the register map, usually NULL.
  503. *
  504. * Returns the regmap for the device if one is present, or NULL. If
  505. * name is specified then it must match the name specified when
  506. * registering the device, if it is NULL then the first regmap found
  507. * will be used. Devices with multiple register maps are very rare,
  508. * generic code should normally not need to specify a name.
  509. */
  510. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  511. {
  512. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  513. dev_get_regmap_match, (void *)name);
  514. if (!r)
  515. return NULL;
  516. return *r;
  517. }
  518. EXPORT_SYMBOL_GPL(dev_get_regmap);
  519. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  520. const void *val, size_t val_len)
  521. {
  522. u8 *u8 = map->work_buf;
  523. void *buf;
  524. int ret = -ENOTSUPP;
  525. size_t len;
  526. int i;
  527. /* Check for unwritable registers before we start */
  528. if (map->writeable_reg)
  529. for (i = 0; i < val_len / map->format.val_bytes; i++)
  530. if (!map->writeable_reg(map->dev,
  531. reg + (i * map->reg_stride)))
  532. return -EINVAL;
  533. if (!map->cache_bypass && map->format.parse_val) {
  534. unsigned int ival;
  535. int val_bytes = map->format.val_bytes;
  536. for (i = 0; i < val_len / val_bytes; i++) {
  537. memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
  538. ival = map->format.parse_val(map->work_buf);
  539. ret = regcache_write(map, reg + (i * map->reg_stride),
  540. ival);
  541. if (ret) {
  542. dev_err(map->dev,
  543. "Error in caching of register: %u ret: %d\n",
  544. reg + i, ret);
  545. return ret;
  546. }
  547. }
  548. if (map->cache_only) {
  549. map->cache_dirty = true;
  550. return 0;
  551. }
  552. }
  553. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  554. u8[0] |= map->write_flag_mask;
  555. trace_regmap_hw_write_start(map->dev, reg,
  556. val_len / map->format.val_bytes);
  557. /* If we're doing a single register write we can probably just
  558. * send the work_buf directly, otherwise try to do a gather
  559. * write.
  560. */
  561. if (val == (map->work_buf + map->format.pad_bytes +
  562. map->format.reg_bytes))
  563. ret = map->bus->write(map->bus_context, map->work_buf,
  564. map->format.reg_bytes +
  565. map->format.pad_bytes +
  566. val_len);
  567. else if (map->bus->gather_write)
  568. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  569. map->format.reg_bytes +
  570. map->format.pad_bytes,
  571. val, val_len);
  572. /* If that didn't work fall back on linearising by hand. */
  573. if (ret == -ENOTSUPP) {
  574. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  575. buf = kzalloc(len, GFP_KERNEL);
  576. if (!buf)
  577. return -ENOMEM;
  578. memcpy(buf, map->work_buf, map->format.reg_bytes);
  579. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  580. val, val_len);
  581. ret = map->bus->write(map->bus_context, buf, len);
  582. kfree(buf);
  583. }
  584. trace_regmap_hw_write_done(map->dev, reg,
  585. val_len / map->format.val_bytes);
  586. return ret;
  587. }
  588. int _regmap_write(struct regmap *map, unsigned int reg,
  589. unsigned int val)
  590. {
  591. int ret;
  592. BUG_ON(!map->format.format_write && !map->format.format_val);
  593. if (!map->cache_bypass && map->format.format_write) {
  594. ret = regcache_write(map, reg, val);
  595. if (ret != 0)
  596. return ret;
  597. if (map->cache_only) {
  598. map->cache_dirty = true;
  599. return 0;
  600. }
  601. }
  602. #ifdef LOG_DEVICE
  603. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  604. dev_info(map->dev, "%x <= %x\n", reg, val);
  605. #endif
  606. trace_regmap_reg_write(map->dev, reg, val);
  607. if (map->format.format_write) {
  608. map->format.format_write(map, reg, val);
  609. trace_regmap_hw_write_start(map->dev, reg, 1);
  610. ret = map->bus->write(map->bus_context, map->work_buf,
  611. map->format.buf_size);
  612. trace_regmap_hw_write_done(map->dev, reg, 1);
  613. return ret;
  614. } else {
  615. map->format.format_val(map->work_buf + map->format.reg_bytes
  616. + map->format.pad_bytes, val, 0);
  617. return _regmap_raw_write(map, reg,
  618. map->work_buf +
  619. map->format.reg_bytes +
  620. map->format.pad_bytes,
  621. map->format.val_bytes);
  622. }
  623. }
  624. /**
  625. * regmap_write(): Write a value to a single register
  626. *
  627. * @map: Register map to write to
  628. * @reg: Register to write to
  629. * @val: Value to be written
  630. *
  631. * A value of zero will be returned on success, a negative errno will
  632. * be returned in error cases.
  633. */
  634. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  635. {
  636. int ret;
  637. if (reg % map->reg_stride)
  638. return -EINVAL;
  639. map->lock(map);
  640. ret = _regmap_write(map, reg, val);
  641. map->unlock(map);
  642. return ret;
  643. }
  644. EXPORT_SYMBOL_GPL(regmap_write);
  645. /**
  646. * regmap_raw_write(): Write raw values to one or more registers
  647. *
  648. * @map: Register map to write to
  649. * @reg: Initial register to write to
  650. * @val: Block of data to be written, laid out for direct transmission to the
  651. * device
  652. * @val_len: Length of data pointed to by val.
  653. *
  654. * This function is intended to be used for things like firmware
  655. * download where a large block of data needs to be transferred to the
  656. * device. No formatting will be done on the data provided.
  657. *
  658. * A value of zero will be returned on success, a negative errno will
  659. * be returned in error cases.
  660. */
  661. int regmap_raw_write(struct regmap *map, unsigned int reg,
  662. const void *val, size_t val_len)
  663. {
  664. int ret;
  665. if (val_len % map->format.val_bytes)
  666. return -EINVAL;
  667. if (reg % map->reg_stride)
  668. return -EINVAL;
  669. map->lock(map);
  670. ret = _regmap_raw_write(map, reg, val, val_len);
  671. map->unlock(map);
  672. return ret;
  673. }
  674. EXPORT_SYMBOL_GPL(regmap_raw_write);
  675. /*
  676. * regmap_bulk_write(): Write multiple registers to the device
  677. *
  678. * @map: Register map to write to
  679. * @reg: First register to be write from
  680. * @val: Block of data to be written, in native register size for device
  681. * @val_count: Number of registers to write
  682. *
  683. * This function is intended to be used for writing a large block of
  684. * data to be device either in single transfer or multiple transfer.
  685. *
  686. * A value of zero will be returned on success, a negative errno will
  687. * be returned in error cases.
  688. */
  689. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  690. size_t val_count)
  691. {
  692. int ret = 0, i;
  693. size_t val_bytes = map->format.val_bytes;
  694. void *wval;
  695. if (!map->format.parse_val)
  696. return -EINVAL;
  697. if (reg % map->reg_stride)
  698. return -EINVAL;
  699. map->lock(map);
  700. /* No formatting is require if val_byte is 1 */
  701. if (val_bytes == 1) {
  702. wval = (void *)val;
  703. } else {
  704. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  705. if (!wval) {
  706. ret = -ENOMEM;
  707. dev_err(map->dev, "Error in memory allocation\n");
  708. goto out;
  709. }
  710. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  711. map->format.parse_val(wval + i);
  712. }
  713. /*
  714. * Some devices does not support bulk write, for
  715. * them we have a series of single write operations.
  716. */
  717. if (map->use_single_rw) {
  718. for (i = 0; i < val_count; i++) {
  719. ret = regmap_raw_write(map,
  720. reg + (i * map->reg_stride),
  721. val + (i * val_bytes),
  722. val_bytes);
  723. if (ret != 0)
  724. return ret;
  725. }
  726. } else {
  727. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
  728. }
  729. if (val_bytes != 1)
  730. kfree(wval);
  731. out:
  732. map->unlock(map);
  733. return ret;
  734. }
  735. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  736. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  737. unsigned int val_len)
  738. {
  739. u8 *u8 = map->work_buf;
  740. int ret;
  741. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  742. /*
  743. * Some buses or devices flag reads by setting the high bits in the
  744. * register addresss; since it's always the high bits for all
  745. * current formats we can do this here rather than in
  746. * formatting. This may break if we get interesting formats.
  747. */
  748. u8[0] |= map->read_flag_mask;
  749. trace_regmap_hw_read_start(map->dev, reg,
  750. val_len / map->format.val_bytes);
  751. ret = map->bus->read(map->bus_context, map->work_buf,
  752. map->format.reg_bytes + map->format.pad_bytes,
  753. val, val_len);
  754. trace_regmap_hw_read_done(map->dev, reg,
  755. val_len / map->format.val_bytes);
  756. return ret;
  757. }
  758. static int _regmap_read(struct regmap *map, unsigned int reg,
  759. unsigned int *val)
  760. {
  761. int ret;
  762. if (!map->cache_bypass) {
  763. ret = regcache_read(map, reg, val);
  764. if (ret == 0)
  765. return 0;
  766. }
  767. if (!map->format.parse_val)
  768. return -EINVAL;
  769. if (map->cache_only)
  770. return -EBUSY;
  771. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  772. if (ret == 0) {
  773. *val = map->format.parse_val(map->work_buf);
  774. #ifdef LOG_DEVICE
  775. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  776. dev_info(map->dev, "%x => %x\n", reg, *val);
  777. #endif
  778. trace_regmap_reg_read(map->dev, reg, *val);
  779. }
  780. if (ret == 0 && !map->cache_bypass)
  781. regcache_write(map, reg, *val);
  782. return ret;
  783. }
  784. /**
  785. * regmap_read(): Read a value from a single register
  786. *
  787. * @map: Register map to write to
  788. * @reg: Register to be read from
  789. * @val: Pointer to store read value
  790. *
  791. * A value of zero will be returned on success, a negative errno will
  792. * be returned in error cases.
  793. */
  794. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  795. {
  796. int ret;
  797. if (reg % map->reg_stride)
  798. return -EINVAL;
  799. map->lock(map);
  800. ret = _regmap_read(map, reg, val);
  801. map->unlock(map);
  802. return ret;
  803. }
  804. EXPORT_SYMBOL_GPL(regmap_read);
  805. /**
  806. * regmap_raw_read(): Read raw data from the device
  807. *
  808. * @map: Register map to write to
  809. * @reg: First register to be read from
  810. * @val: Pointer to store read value
  811. * @val_len: Size of data to read
  812. *
  813. * A value of zero will be returned on success, a negative errno will
  814. * be returned in error cases.
  815. */
  816. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  817. size_t val_len)
  818. {
  819. size_t val_bytes = map->format.val_bytes;
  820. size_t val_count = val_len / val_bytes;
  821. unsigned int v;
  822. int ret, i;
  823. if (val_len % map->format.val_bytes)
  824. return -EINVAL;
  825. if (reg % map->reg_stride)
  826. return -EINVAL;
  827. map->lock(map);
  828. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  829. map->cache_type == REGCACHE_NONE) {
  830. /* Physical block read if there's no cache involved */
  831. ret = _regmap_raw_read(map, reg, val, val_len);
  832. } else {
  833. /* Otherwise go word by word for the cache; should be low
  834. * cost as we expect to hit the cache.
  835. */
  836. for (i = 0; i < val_count; i++) {
  837. ret = _regmap_read(map, reg + (i * map->reg_stride),
  838. &v);
  839. if (ret != 0)
  840. goto out;
  841. map->format.format_val(val + (i * val_bytes), v, 0);
  842. }
  843. }
  844. out:
  845. map->unlock(map);
  846. return ret;
  847. }
  848. EXPORT_SYMBOL_GPL(regmap_raw_read);
  849. /**
  850. * regmap_bulk_read(): Read multiple registers from the device
  851. *
  852. * @map: Register map to write to
  853. * @reg: First register to be read from
  854. * @val: Pointer to store read value, in native register size for device
  855. * @val_count: Number of registers to read
  856. *
  857. * A value of zero will be returned on success, a negative errno will
  858. * be returned in error cases.
  859. */
  860. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  861. size_t val_count)
  862. {
  863. int ret, i;
  864. size_t val_bytes = map->format.val_bytes;
  865. bool vol = regmap_volatile_range(map, reg, val_count);
  866. if (!map->format.parse_val)
  867. return -EINVAL;
  868. if (reg % map->reg_stride)
  869. return -EINVAL;
  870. if (vol || map->cache_type == REGCACHE_NONE) {
  871. /*
  872. * Some devices does not support bulk read, for
  873. * them we have a series of single read operations.
  874. */
  875. if (map->use_single_rw) {
  876. for (i = 0; i < val_count; i++) {
  877. ret = regmap_raw_read(map,
  878. reg + (i * map->reg_stride),
  879. val + (i * val_bytes),
  880. val_bytes);
  881. if (ret != 0)
  882. return ret;
  883. }
  884. } else {
  885. ret = regmap_raw_read(map, reg, val,
  886. val_bytes * val_count);
  887. if (ret != 0)
  888. return ret;
  889. }
  890. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  891. map->format.parse_val(val + i);
  892. } else {
  893. for (i = 0; i < val_count; i++) {
  894. unsigned int ival;
  895. ret = regmap_read(map, reg + (i * map->reg_stride),
  896. &ival);
  897. if (ret != 0)
  898. return ret;
  899. memcpy(val + (i * val_bytes), &ival, val_bytes);
  900. }
  901. }
  902. return 0;
  903. }
  904. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  905. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  906. unsigned int mask, unsigned int val,
  907. bool *change)
  908. {
  909. int ret;
  910. unsigned int tmp, orig;
  911. map->lock(map);
  912. ret = _regmap_read(map, reg, &orig);
  913. if (ret != 0)
  914. goto out;
  915. tmp = orig & ~mask;
  916. tmp |= val & mask;
  917. if (tmp != orig) {
  918. ret = _regmap_write(map, reg, tmp);
  919. *change = true;
  920. } else {
  921. *change = false;
  922. }
  923. out:
  924. map->unlock(map);
  925. return ret;
  926. }
  927. /**
  928. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  929. *
  930. * @map: Register map to update
  931. * @reg: Register to update
  932. * @mask: Bitmask to change
  933. * @val: New value for bitmask
  934. *
  935. * Returns zero for success, a negative number on error.
  936. */
  937. int regmap_update_bits(struct regmap *map, unsigned int reg,
  938. unsigned int mask, unsigned int val)
  939. {
  940. bool change;
  941. return _regmap_update_bits(map, reg, mask, val, &change);
  942. }
  943. EXPORT_SYMBOL_GPL(regmap_update_bits);
  944. /**
  945. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  946. * register map and report if updated
  947. *
  948. * @map: Register map to update
  949. * @reg: Register to update
  950. * @mask: Bitmask to change
  951. * @val: New value for bitmask
  952. * @change: Boolean indicating if a write was done
  953. *
  954. * Returns zero for success, a negative number on error.
  955. */
  956. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  957. unsigned int mask, unsigned int val,
  958. bool *change)
  959. {
  960. return _regmap_update_bits(map, reg, mask, val, change);
  961. }
  962. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  963. /**
  964. * regmap_register_patch: Register and apply register updates to be applied
  965. * on device initialistion
  966. *
  967. * @map: Register map to apply updates to.
  968. * @regs: Values to update.
  969. * @num_regs: Number of entries in regs.
  970. *
  971. * Register a set of register updates to be applied to the device
  972. * whenever the device registers are synchronised with the cache and
  973. * apply them immediately. Typically this is used to apply
  974. * corrections to be applied to the device defaults on startup, such
  975. * as the updates some vendors provide to undocumented registers.
  976. */
  977. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  978. int num_regs)
  979. {
  980. int i, ret;
  981. bool bypass;
  982. /* If needed the implementation can be extended to support this */
  983. if (map->patch)
  984. return -EBUSY;
  985. map->lock(map);
  986. bypass = map->cache_bypass;
  987. map->cache_bypass = true;
  988. /* Write out first; it's useful to apply even if we fail later. */
  989. for (i = 0; i < num_regs; i++) {
  990. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  991. if (ret != 0) {
  992. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  993. regs[i].reg, regs[i].def, ret);
  994. goto out;
  995. }
  996. }
  997. map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
  998. if (map->patch != NULL) {
  999. memcpy(map->patch, regs,
  1000. num_regs * sizeof(struct reg_default));
  1001. map->patch_regs = num_regs;
  1002. } else {
  1003. ret = -ENOMEM;
  1004. }
  1005. out:
  1006. map->cache_bypass = bypass;
  1007. map->unlock(map);
  1008. return ret;
  1009. }
  1010. EXPORT_SYMBOL_GPL(regmap_register_patch);
  1011. /*
  1012. * regmap_get_val_bytes(): Report the size of a register value
  1013. *
  1014. * Report the size of a register value, mainly intended to for use by
  1015. * generic infrastructure built on top of regmap.
  1016. */
  1017. int regmap_get_val_bytes(struct regmap *map)
  1018. {
  1019. if (map->format.format_write)
  1020. return -EINVAL;
  1021. return map->format.val_bytes;
  1022. }
  1023. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  1024. static int __init regmap_initcall(void)
  1025. {
  1026. regmap_debugfs_initcall();
  1027. return 0;
  1028. }
  1029. postcore_initcall(regmap_initcall);