core.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * nvmem framework core.
  4. *
  5. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  6. * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/export.h>
  10. #include <linux/fs.h>
  11. #include <linux/idr.h>
  12. #include <linux/init.h>
  13. #include <linux/kref.h>
  14. #include <linux/module.h>
  15. #include <linux/nvmem-consumer.h>
  16. #include <linux/nvmem-provider.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/of.h>
  19. #include <linux/slab.h>
  20. struct nvmem_device {
  21. struct module *owner;
  22. struct device dev;
  23. int stride;
  24. int word_size;
  25. int id;
  26. struct kref refcnt;
  27. size_t size;
  28. bool read_only;
  29. bool root_only;
  30. int flags;
  31. enum nvmem_type type;
  32. struct bin_attribute eeprom;
  33. struct device *base_dev;
  34. struct list_head cells;
  35. nvmem_reg_read_t reg_read;
  36. nvmem_reg_write_t reg_write;
  37. struct gpio_desc *wp_gpio;
  38. void *priv;
  39. };
  40. #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  41. #define FLAG_COMPAT BIT(0)
  42. struct nvmem_cell {
  43. const char *name;
  44. int offset;
  45. int bytes;
  46. int bit_offset;
  47. int nbits;
  48. struct device_node *np;
  49. struct nvmem_device *nvmem;
  50. struct list_head node;
  51. };
  52. static DEFINE_MUTEX(nvmem_mutex);
  53. static DEFINE_IDA(nvmem_ida);
  54. static DEFINE_MUTEX(nvmem_cell_mutex);
  55. static LIST_HEAD(nvmem_cell_tables);
  56. static DEFINE_MUTEX(nvmem_lookup_mutex);
  57. static LIST_HEAD(nvmem_lookup_list);
  58. static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
  59. static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  60. void *val, size_t bytes)
  61. {
  62. if (nvmem->reg_read)
  63. return nvmem->reg_read(nvmem->priv, offset, val, bytes);
  64. return -EINVAL;
  65. }
  66. static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  67. void *val, size_t bytes)
  68. {
  69. int ret;
  70. if (nvmem->reg_write) {
  71. gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
  72. ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
  73. gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
  74. return ret;
  75. }
  76. return -EINVAL;
  77. }
  78. #ifdef CONFIG_NVMEM_SYSFS
  79. static const char * const nvmem_type_str[] = {
  80. [NVMEM_TYPE_UNKNOWN] = "Unknown",
  81. [NVMEM_TYPE_EEPROM] = "EEPROM",
  82. [NVMEM_TYPE_OTP] = "OTP",
  83. [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
  84. };
  85. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  86. static struct lock_class_key eeprom_lock_key;
  87. #endif
  88. static ssize_t type_show(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. struct nvmem_device *nvmem = to_nvmem_device(dev);
  92. return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
  93. }
  94. static DEVICE_ATTR_RO(type);
  95. static struct attribute *nvmem_attrs[] = {
  96. &dev_attr_type.attr,
  97. NULL,
  98. };
  99. static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
  100. struct bin_attribute *attr, char *buf,
  101. loff_t pos, size_t count)
  102. {
  103. struct device *dev;
  104. struct nvmem_device *nvmem;
  105. int rc;
  106. if (attr->private)
  107. dev = attr->private;
  108. else
  109. dev = kobj_to_dev(kobj);
  110. nvmem = to_nvmem_device(dev);
  111. /* Stop the user from reading */
  112. if (pos >= nvmem->size)
  113. return 0;
  114. if (!IS_ALIGNED(pos, nvmem->stride))
  115. return -EINVAL;
  116. if (count < nvmem->word_size)
  117. return -EINVAL;
  118. if (pos + count > nvmem->size)
  119. count = nvmem->size - pos;
  120. count = round_down(count, nvmem->word_size);
  121. if (!nvmem->reg_read)
  122. return -EPERM;
  123. rc = nvmem_reg_read(nvmem, pos, buf, count);
  124. if (rc)
  125. return rc;
  126. return count;
  127. }
  128. static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
  129. struct bin_attribute *attr, char *buf,
  130. loff_t pos, size_t count)
  131. {
  132. struct device *dev;
  133. struct nvmem_device *nvmem;
  134. int rc;
  135. if (attr->private)
  136. dev = attr->private;
  137. else
  138. dev = kobj_to_dev(kobj);
  139. nvmem = to_nvmem_device(dev);
  140. /* Stop the user from writing */
  141. if (pos >= nvmem->size)
  142. return -EFBIG;
  143. if (!IS_ALIGNED(pos, nvmem->stride))
  144. return -EINVAL;
  145. if (count < nvmem->word_size)
  146. return -EINVAL;
  147. if (pos + count > nvmem->size)
  148. count = nvmem->size - pos;
  149. count = round_down(count, nvmem->word_size);
  150. if (!nvmem->reg_write)
  151. return -EPERM;
  152. rc = nvmem_reg_write(nvmem, pos, buf, count);
  153. if (rc)
  154. return rc;
  155. return count;
  156. }
  157. static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
  158. {
  159. umode_t mode = 0400;
  160. if (!nvmem->root_only)
  161. mode |= 0044;
  162. if (!nvmem->read_only)
  163. mode |= 0200;
  164. if (!nvmem->reg_write)
  165. mode &= ~0200;
  166. if (!nvmem->reg_read)
  167. mode &= ~0444;
  168. return mode;
  169. }
  170. static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
  171. struct bin_attribute *attr, int i)
  172. {
  173. struct device *dev = kobj_to_dev(kobj);
  174. struct nvmem_device *nvmem = to_nvmem_device(dev);
  175. attr->size = nvmem->size;
  176. return nvmem_bin_attr_get_umode(nvmem);
  177. }
  178. /* default read/write permissions */
  179. static struct bin_attribute bin_attr_rw_nvmem = {
  180. .attr = {
  181. .name = "nvmem",
  182. .mode = 0644,
  183. },
  184. .read = bin_attr_nvmem_read,
  185. .write = bin_attr_nvmem_write,
  186. };
  187. static struct bin_attribute *nvmem_bin_attributes[] = {
  188. &bin_attr_rw_nvmem,
  189. NULL,
  190. };
  191. static const struct attribute_group nvmem_bin_group = {
  192. .bin_attrs = nvmem_bin_attributes,
  193. .attrs = nvmem_attrs,
  194. .is_bin_visible = nvmem_bin_attr_is_visible,
  195. };
  196. static const struct attribute_group *nvmem_dev_groups[] = {
  197. &nvmem_bin_group,
  198. NULL,
  199. };
  200. static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
  201. .attr = {
  202. .name = "eeprom",
  203. },
  204. .read = bin_attr_nvmem_read,
  205. .write = bin_attr_nvmem_write,
  206. };
  207. /*
  208. * nvmem_setup_compat() - Create an additional binary entry in
  209. * drivers sys directory, to be backwards compatible with the older
  210. * drivers/misc/eeprom drivers.
  211. */
  212. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  213. const struct nvmem_config *config)
  214. {
  215. int rval;
  216. if (!config->compat)
  217. return 0;
  218. if (!config->base_dev)
  219. return -EINVAL;
  220. nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
  221. nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
  222. nvmem->eeprom.size = nvmem->size;
  223. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  224. nvmem->eeprom.attr.key = &eeprom_lock_key;
  225. #endif
  226. nvmem->eeprom.private = &nvmem->dev;
  227. nvmem->base_dev = config->base_dev;
  228. rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
  229. if (rval) {
  230. dev_err(&nvmem->dev,
  231. "Failed to create eeprom binary file %d\n", rval);
  232. return rval;
  233. }
  234. nvmem->flags |= FLAG_COMPAT;
  235. return 0;
  236. }
  237. static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
  238. const struct nvmem_config *config)
  239. {
  240. if (config->compat)
  241. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  242. }
  243. #else /* CONFIG_NVMEM_SYSFS */
  244. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  245. const struct nvmem_config *config)
  246. {
  247. return -ENOSYS;
  248. }
  249. static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
  250. const struct nvmem_config *config)
  251. {
  252. }
  253. #endif /* CONFIG_NVMEM_SYSFS */
  254. static void nvmem_release(struct device *dev)
  255. {
  256. struct nvmem_device *nvmem = to_nvmem_device(dev);
  257. ida_free(&nvmem_ida, nvmem->id);
  258. gpiod_put(nvmem->wp_gpio);
  259. kfree(nvmem);
  260. }
  261. static const struct device_type nvmem_provider_type = {
  262. .release = nvmem_release,
  263. };
  264. static struct bus_type nvmem_bus_type = {
  265. .name = "nvmem",
  266. };
  267. static void nvmem_cell_drop(struct nvmem_cell *cell)
  268. {
  269. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
  270. mutex_lock(&nvmem_mutex);
  271. list_del(&cell->node);
  272. mutex_unlock(&nvmem_mutex);
  273. of_node_put(cell->np);
  274. kfree_const(cell->name);
  275. kfree(cell);
  276. }
  277. static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
  278. {
  279. struct nvmem_cell *cell, *p;
  280. list_for_each_entry_safe(cell, p, &nvmem->cells, node)
  281. nvmem_cell_drop(cell);
  282. }
  283. static void nvmem_cell_add(struct nvmem_cell *cell)
  284. {
  285. mutex_lock(&nvmem_mutex);
  286. list_add_tail(&cell->node, &cell->nvmem->cells);
  287. mutex_unlock(&nvmem_mutex);
  288. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
  289. }
  290. static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem,
  291. const struct nvmem_cell_info *info,
  292. struct nvmem_cell *cell)
  293. {
  294. cell->nvmem = nvmem;
  295. cell->offset = info->offset;
  296. cell->bytes = info->bytes;
  297. cell->name = info->name;
  298. cell->bit_offset = info->bit_offset;
  299. cell->nbits = info->nbits;
  300. if (cell->nbits)
  301. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  302. BITS_PER_BYTE);
  303. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  304. dev_err(&nvmem->dev,
  305. "cell %s unaligned to nvmem stride %d\n",
  306. cell->name ?: "<unknown>", nvmem->stride);
  307. return -EINVAL;
  308. }
  309. return 0;
  310. }
  311. static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
  312. const struct nvmem_cell_info *info,
  313. struct nvmem_cell *cell)
  314. {
  315. int err;
  316. err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell);
  317. if (err)
  318. return err;
  319. cell->name = kstrdup_const(info->name, GFP_KERNEL);
  320. if (!cell->name)
  321. return -ENOMEM;
  322. return 0;
  323. }
  324. /**
  325. * nvmem_add_cells() - Add cell information to an nvmem device
  326. *
  327. * @nvmem: nvmem device to add cells to.
  328. * @info: nvmem cell info to add to the device
  329. * @ncells: number of cells in info
  330. *
  331. * Return: 0 or negative error code on failure.
  332. */
  333. static int nvmem_add_cells(struct nvmem_device *nvmem,
  334. const struct nvmem_cell_info *info,
  335. int ncells)
  336. {
  337. struct nvmem_cell **cells;
  338. int i, rval;
  339. cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
  340. if (!cells)
  341. return -ENOMEM;
  342. for (i = 0; i < ncells; i++) {
  343. cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
  344. if (!cells[i]) {
  345. rval = -ENOMEM;
  346. goto err;
  347. }
  348. rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
  349. if (rval) {
  350. kfree(cells[i]);
  351. goto err;
  352. }
  353. nvmem_cell_add(cells[i]);
  354. }
  355. /* remove tmp array */
  356. kfree(cells);
  357. return 0;
  358. err:
  359. while (i--)
  360. nvmem_cell_drop(cells[i]);
  361. kfree(cells);
  362. return rval;
  363. }
  364. /**
  365. * nvmem_register_notifier() - Register a notifier block for nvmem events.
  366. *
  367. * @nb: notifier block to be called on nvmem events.
  368. *
  369. * Return: 0 on success, negative error number on failure.
  370. */
  371. int nvmem_register_notifier(struct notifier_block *nb)
  372. {
  373. return blocking_notifier_chain_register(&nvmem_notifier, nb);
  374. }
  375. EXPORT_SYMBOL_GPL(nvmem_register_notifier);
  376. /**
  377. * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
  378. *
  379. * @nb: notifier block to be unregistered.
  380. *
  381. * Return: 0 on success, negative error number on failure.
  382. */
  383. int nvmem_unregister_notifier(struct notifier_block *nb)
  384. {
  385. return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
  386. }
  387. EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
  388. static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
  389. {
  390. const struct nvmem_cell_info *info;
  391. struct nvmem_cell_table *table;
  392. struct nvmem_cell *cell;
  393. int rval = 0, i;
  394. mutex_lock(&nvmem_cell_mutex);
  395. list_for_each_entry(table, &nvmem_cell_tables, node) {
  396. if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
  397. for (i = 0; i < table->ncells; i++) {
  398. info = &table->cells[i];
  399. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  400. if (!cell) {
  401. rval = -ENOMEM;
  402. goto out;
  403. }
  404. rval = nvmem_cell_info_to_nvmem_cell(nvmem,
  405. info,
  406. cell);
  407. if (rval) {
  408. kfree(cell);
  409. goto out;
  410. }
  411. nvmem_cell_add(cell);
  412. }
  413. }
  414. }
  415. out:
  416. mutex_unlock(&nvmem_cell_mutex);
  417. return rval;
  418. }
  419. static struct nvmem_cell *
  420. nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
  421. {
  422. struct nvmem_cell *iter, *cell = NULL;
  423. mutex_lock(&nvmem_mutex);
  424. list_for_each_entry(iter, &nvmem->cells, node) {
  425. if (strcmp(cell_id, iter->name) == 0) {
  426. cell = iter;
  427. break;
  428. }
  429. }
  430. mutex_unlock(&nvmem_mutex);
  431. return cell;
  432. }
  433. static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
  434. {
  435. struct device_node *parent, *child;
  436. struct device *dev = &nvmem->dev;
  437. struct nvmem_cell *cell;
  438. const __be32 *addr;
  439. int len;
  440. parent = dev->of_node;
  441. for_each_child_of_node(parent, child) {
  442. addr = of_get_property(child, "reg", &len);
  443. if (!addr)
  444. continue;
  445. if (len < 2 * sizeof(u32)) {
  446. dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
  447. of_node_put(child);
  448. return -EINVAL;
  449. }
  450. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  451. if (!cell) {
  452. of_node_put(child);
  453. return -ENOMEM;
  454. }
  455. cell->nvmem = nvmem;
  456. cell->offset = be32_to_cpup(addr++);
  457. cell->bytes = be32_to_cpup(addr);
  458. cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
  459. addr = of_get_property(child, "bits", &len);
  460. if (addr && len == (2 * sizeof(u32))) {
  461. cell->bit_offset = be32_to_cpup(addr++);
  462. cell->nbits = be32_to_cpup(addr);
  463. }
  464. if (cell->nbits)
  465. cell->bytes = DIV_ROUND_UP(
  466. cell->nbits + cell->bit_offset,
  467. BITS_PER_BYTE);
  468. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  469. dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
  470. cell->name, nvmem->stride);
  471. /* Cells already added will be freed later. */
  472. kfree_const(cell->name);
  473. kfree(cell);
  474. of_node_put(child);
  475. return -EINVAL;
  476. }
  477. cell->np = of_node_get(child);
  478. nvmem_cell_add(cell);
  479. }
  480. return 0;
  481. }
  482. /**
  483. * nvmem_register() - Register a nvmem device for given nvmem_config.
  484. * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  485. *
  486. * @config: nvmem device configuration with which nvmem device is created.
  487. *
  488. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  489. * on success.
  490. */
  491. struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  492. {
  493. struct nvmem_device *nvmem;
  494. int rval;
  495. if (!config->dev)
  496. return ERR_PTR(-EINVAL);
  497. if (!config->reg_read && !config->reg_write)
  498. return ERR_PTR(-EINVAL);
  499. nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
  500. if (!nvmem)
  501. return ERR_PTR(-ENOMEM);
  502. rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
  503. if (rval < 0) {
  504. kfree(nvmem);
  505. return ERR_PTR(rval);
  506. }
  507. if (config->wp_gpio)
  508. nvmem->wp_gpio = config->wp_gpio;
  509. else
  510. nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
  511. GPIOD_OUT_HIGH);
  512. if (IS_ERR(nvmem->wp_gpio)) {
  513. ida_free(&nvmem_ida, nvmem->id);
  514. rval = PTR_ERR(nvmem->wp_gpio);
  515. kfree(nvmem);
  516. return ERR_PTR(rval);
  517. }
  518. kref_init(&nvmem->refcnt);
  519. INIT_LIST_HEAD(&nvmem->cells);
  520. nvmem->id = rval;
  521. nvmem->owner = config->owner;
  522. if (!nvmem->owner && config->dev->driver)
  523. nvmem->owner = config->dev->driver->owner;
  524. nvmem->stride = config->stride ?: 1;
  525. nvmem->word_size = config->word_size ?: 1;
  526. nvmem->size = config->size;
  527. nvmem->dev.type = &nvmem_provider_type;
  528. nvmem->dev.bus = &nvmem_bus_type;
  529. nvmem->dev.parent = config->dev;
  530. nvmem->root_only = config->root_only;
  531. nvmem->priv = config->priv;
  532. nvmem->type = config->type;
  533. nvmem->reg_read = config->reg_read;
  534. nvmem->reg_write = config->reg_write;
  535. if (!config->no_of_node)
  536. nvmem->dev.of_node = config->dev->of_node;
  537. switch (config->id) {
  538. case NVMEM_DEVID_NONE:
  539. dev_set_name(&nvmem->dev, "%s", config->name);
  540. break;
  541. case NVMEM_DEVID_AUTO:
  542. dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
  543. break;
  544. default:
  545. dev_set_name(&nvmem->dev, "%s%d",
  546. config->name ? : "nvmem",
  547. config->name ? config->id : nvmem->id);
  548. break;
  549. }
  550. nvmem->read_only = device_property_present(config->dev, "read-only") ||
  551. config->read_only || !nvmem->reg_write;
  552. #ifdef CONFIG_NVMEM_SYSFS
  553. nvmem->dev.groups = nvmem_dev_groups;
  554. #endif
  555. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  556. rval = device_register(&nvmem->dev);
  557. if (rval)
  558. goto err_put_device;
  559. if (config->compat) {
  560. rval = nvmem_sysfs_setup_compat(nvmem, config);
  561. if (rval)
  562. goto err_device_del;
  563. }
  564. if (config->cells) {
  565. rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
  566. if (rval)
  567. goto err_teardown_compat;
  568. }
  569. rval = nvmem_add_cells_from_table(nvmem);
  570. if (rval)
  571. goto err_remove_cells;
  572. rval = nvmem_add_cells_from_of(nvmem);
  573. if (rval)
  574. goto err_remove_cells;
  575. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
  576. return nvmem;
  577. err_remove_cells:
  578. nvmem_device_remove_all_cells(nvmem);
  579. err_teardown_compat:
  580. if (config->compat)
  581. nvmem_sysfs_remove_compat(nvmem, config);
  582. err_device_del:
  583. device_del(&nvmem->dev);
  584. err_put_device:
  585. put_device(&nvmem->dev);
  586. return ERR_PTR(rval);
  587. }
  588. EXPORT_SYMBOL_GPL(nvmem_register);
  589. static void nvmem_device_release(struct kref *kref)
  590. {
  591. struct nvmem_device *nvmem;
  592. nvmem = container_of(kref, struct nvmem_device, refcnt);
  593. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
  594. if (nvmem->flags & FLAG_COMPAT)
  595. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  596. nvmem_device_remove_all_cells(nvmem);
  597. device_unregister(&nvmem->dev);
  598. }
  599. /**
  600. * nvmem_unregister() - Unregister previously registered nvmem device
  601. *
  602. * @nvmem: Pointer to previously registered nvmem device.
  603. */
  604. void nvmem_unregister(struct nvmem_device *nvmem)
  605. {
  606. kref_put(&nvmem->refcnt, nvmem_device_release);
  607. }
  608. EXPORT_SYMBOL_GPL(nvmem_unregister);
  609. static void devm_nvmem_release(struct device *dev, void *res)
  610. {
  611. nvmem_unregister(*(struct nvmem_device **)res);
  612. }
  613. /**
  614. * devm_nvmem_register() - Register a managed nvmem device for given
  615. * nvmem_config.
  616. * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  617. *
  618. * @dev: Device that uses the nvmem device.
  619. * @config: nvmem device configuration with which nvmem device is created.
  620. *
  621. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  622. * on success.
  623. */
  624. struct nvmem_device *devm_nvmem_register(struct device *dev,
  625. const struct nvmem_config *config)
  626. {
  627. struct nvmem_device **ptr, *nvmem;
  628. ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
  629. if (!ptr)
  630. return ERR_PTR(-ENOMEM);
  631. nvmem = nvmem_register(config);
  632. if (!IS_ERR(nvmem)) {
  633. *ptr = nvmem;
  634. devres_add(dev, ptr);
  635. } else {
  636. devres_free(ptr);
  637. }
  638. return nvmem;
  639. }
  640. EXPORT_SYMBOL_GPL(devm_nvmem_register);
  641. static int devm_nvmem_match(struct device *dev, void *res, void *data)
  642. {
  643. struct nvmem_device **r = res;
  644. return *r == data;
  645. }
  646. /**
  647. * devm_nvmem_unregister() - Unregister previously registered managed nvmem
  648. * device.
  649. *
  650. * @dev: Device that uses the nvmem device.
  651. * @nvmem: Pointer to previously registered nvmem device.
  652. *
  653. * Return: Will be negative on error or zero on success.
  654. */
  655. int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
  656. {
  657. return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
  658. }
  659. EXPORT_SYMBOL(devm_nvmem_unregister);
  660. static struct nvmem_device *__nvmem_device_get(void *data,
  661. int (*match)(struct device *dev, const void *data))
  662. {
  663. struct nvmem_device *nvmem = NULL;
  664. struct device *dev;
  665. mutex_lock(&nvmem_mutex);
  666. dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
  667. if (dev)
  668. nvmem = to_nvmem_device(dev);
  669. mutex_unlock(&nvmem_mutex);
  670. if (!nvmem)
  671. return ERR_PTR(-EPROBE_DEFER);
  672. if (!try_module_get(nvmem->owner)) {
  673. dev_err(&nvmem->dev,
  674. "could not increase module refcount for cell %s\n",
  675. nvmem_dev_name(nvmem));
  676. put_device(&nvmem->dev);
  677. return ERR_PTR(-EINVAL);
  678. }
  679. kref_get(&nvmem->refcnt);
  680. return nvmem;
  681. }
  682. static void __nvmem_device_put(struct nvmem_device *nvmem)
  683. {
  684. put_device(&nvmem->dev);
  685. module_put(nvmem->owner);
  686. kref_put(&nvmem->refcnt, nvmem_device_release);
  687. }
  688. #if IS_ENABLED(CONFIG_OF)
  689. /**
  690. * of_nvmem_device_get() - Get nvmem device from a given id
  691. *
  692. * @np: Device tree node that uses the nvmem device.
  693. * @id: nvmem name from nvmem-names property.
  694. *
  695. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  696. * on success.
  697. */
  698. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  699. {
  700. struct device_node *nvmem_np;
  701. struct nvmem_device *nvmem;
  702. int index = 0;
  703. if (id)
  704. index = of_property_match_string(np, "nvmem-names", id);
  705. nvmem_np = of_parse_phandle(np, "nvmem", index);
  706. if (!nvmem_np)
  707. return ERR_PTR(-ENOENT);
  708. nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
  709. of_node_put(nvmem_np);
  710. return nvmem;
  711. }
  712. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  713. #endif
  714. /**
  715. * nvmem_device_get() - Get nvmem device from a given id
  716. *
  717. * @dev: Device that uses the nvmem device.
  718. * @dev_name: name of the requested nvmem device.
  719. *
  720. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  721. * on success.
  722. */
  723. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  724. {
  725. if (dev->of_node) { /* try dt first */
  726. struct nvmem_device *nvmem;
  727. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  728. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  729. return nvmem;
  730. }
  731. return __nvmem_device_get((void *)dev_name, device_match_name);
  732. }
  733. EXPORT_SYMBOL_GPL(nvmem_device_get);
  734. /**
  735. * nvmem_device_find() - Find nvmem device with matching function
  736. *
  737. * @data: Data to pass to match function
  738. * @match: Callback function to check device
  739. *
  740. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  741. * on success.
  742. */
  743. struct nvmem_device *nvmem_device_find(void *data,
  744. int (*match)(struct device *dev, const void *data))
  745. {
  746. return __nvmem_device_get(data, match);
  747. }
  748. EXPORT_SYMBOL_GPL(nvmem_device_find);
  749. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  750. {
  751. struct nvmem_device **nvmem = res;
  752. if (WARN_ON(!nvmem || !*nvmem))
  753. return 0;
  754. return *nvmem == data;
  755. }
  756. static void devm_nvmem_device_release(struct device *dev, void *res)
  757. {
  758. nvmem_device_put(*(struct nvmem_device **)res);
  759. }
  760. /**
  761. * devm_nvmem_device_put() - put alredy got nvmem device
  762. *
  763. * @dev: Device that uses the nvmem device.
  764. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  765. * that needs to be released.
  766. */
  767. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  768. {
  769. int ret;
  770. ret = devres_release(dev, devm_nvmem_device_release,
  771. devm_nvmem_device_match, nvmem);
  772. WARN_ON(ret);
  773. }
  774. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  775. /**
  776. * nvmem_device_put() - put alredy got nvmem device
  777. *
  778. * @nvmem: pointer to nvmem device that needs to be released.
  779. */
  780. void nvmem_device_put(struct nvmem_device *nvmem)
  781. {
  782. __nvmem_device_put(nvmem);
  783. }
  784. EXPORT_SYMBOL_GPL(nvmem_device_put);
  785. /**
  786. * devm_nvmem_device_get() - Get nvmem cell of device form a given id
  787. *
  788. * @dev: Device that requests the nvmem device.
  789. * @id: name id for the requested nvmem device.
  790. *
  791. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
  792. * on success. The nvmem_cell will be freed by the automatically once the
  793. * device is freed.
  794. */
  795. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  796. {
  797. struct nvmem_device **ptr, *nvmem;
  798. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  799. if (!ptr)
  800. return ERR_PTR(-ENOMEM);
  801. nvmem = nvmem_device_get(dev, id);
  802. if (!IS_ERR(nvmem)) {
  803. *ptr = nvmem;
  804. devres_add(dev, ptr);
  805. } else {
  806. devres_free(ptr);
  807. }
  808. return nvmem;
  809. }
  810. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  811. static struct nvmem_cell *
  812. nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
  813. {
  814. struct nvmem_cell *cell = ERR_PTR(-ENOENT);
  815. struct nvmem_cell_lookup *lookup;
  816. struct nvmem_device *nvmem;
  817. const char *dev_id;
  818. if (!dev)
  819. return ERR_PTR(-EINVAL);
  820. dev_id = dev_name(dev);
  821. mutex_lock(&nvmem_lookup_mutex);
  822. list_for_each_entry(lookup, &nvmem_lookup_list, node) {
  823. if ((strcmp(lookup->dev_id, dev_id) == 0) &&
  824. (strcmp(lookup->con_id, con_id) == 0)) {
  825. /* This is the right entry. */
  826. nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
  827. device_match_name);
  828. if (IS_ERR(nvmem)) {
  829. /* Provider may not be registered yet. */
  830. cell = ERR_CAST(nvmem);
  831. break;
  832. }
  833. cell = nvmem_find_cell_by_name(nvmem,
  834. lookup->cell_name);
  835. if (!cell) {
  836. __nvmem_device_put(nvmem);
  837. cell = ERR_PTR(-ENOENT);
  838. }
  839. break;
  840. }
  841. }
  842. mutex_unlock(&nvmem_lookup_mutex);
  843. return cell;
  844. }
  845. #if IS_ENABLED(CONFIG_OF)
  846. static struct nvmem_cell *
  847. nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
  848. {
  849. struct nvmem_cell *iter, *cell = NULL;
  850. mutex_lock(&nvmem_mutex);
  851. list_for_each_entry(iter, &nvmem->cells, node) {
  852. if (np == iter->np) {
  853. cell = iter;
  854. break;
  855. }
  856. }
  857. mutex_unlock(&nvmem_mutex);
  858. return cell;
  859. }
  860. /**
  861. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  862. *
  863. * @np: Device tree node that uses the nvmem cell.
  864. * @id: nvmem cell name from nvmem-cell-names property, or NULL
  865. * for the cell at index 0 (the lone cell with no accompanying
  866. * nvmem-cell-names property).
  867. *
  868. * Return: Will be an ERR_PTR() on error or a valid pointer
  869. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  870. * nvmem_cell_put().
  871. */
  872. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
  873. {
  874. struct device_node *cell_np, *nvmem_np;
  875. struct nvmem_device *nvmem;
  876. struct nvmem_cell *cell;
  877. int index = 0;
  878. /* if cell name exists, find index to the name */
  879. if (id)
  880. index = of_property_match_string(np, "nvmem-cell-names", id);
  881. cell_np = of_parse_phandle(np, "nvmem-cells", index);
  882. if (!cell_np)
  883. return ERR_PTR(-ENOENT);
  884. nvmem_np = of_get_next_parent(cell_np);
  885. if (!nvmem_np)
  886. return ERR_PTR(-EINVAL);
  887. nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
  888. of_node_put(nvmem_np);
  889. if (IS_ERR(nvmem))
  890. return ERR_CAST(nvmem);
  891. cell = nvmem_find_cell_by_node(nvmem, cell_np);
  892. if (!cell) {
  893. __nvmem_device_put(nvmem);
  894. return ERR_PTR(-ENOENT);
  895. }
  896. return cell;
  897. }
  898. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  899. #endif
  900. /**
  901. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  902. *
  903. * @dev: Device that requests the nvmem cell.
  904. * @id: nvmem cell name to get (this corresponds with the name from the
  905. * nvmem-cell-names property for DT systems and with the con_id from
  906. * the lookup entry for non-DT systems).
  907. *
  908. * Return: Will be an ERR_PTR() on error or a valid pointer
  909. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  910. * nvmem_cell_put().
  911. */
  912. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
  913. {
  914. struct nvmem_cell *cell;
  915. if (dev->of_node) { /* try dt first */
  916. cell = of_nvmem_cell_get(dev->of_node, id);
  917. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  918. return cell;
  919. }
  920. /* NULL cell id only allowed for device tree; invalid otherwise */
  921. if (!id)
  922. return ERR_PTR(-EINVAL);
  923. return nvmem_cell_get_from_lookup(dev, id);
  924. }
  925. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  926. static void devm_nvmem_cell_release(struct device *dev, void *res)
  927. {
  928. nvmem_cell_put(*(struct nvmem_cell **)res);
  929. }
  930. /**
  931. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  932. *
  933. * @dev: Device that requests the nvmem cell.
  934. * @id: nvmem cell name id to get.
  935. *
  936. * Return: Will be an ERR_PTR() on error or a valid pointer
  937. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  938. * automatically once the device is freed.
  939. */
  940. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  941. {
  942. struct nvmem_cell **ptr, *cell;
  943. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  944. if (!ptr)
  945. return ERR_PTR(-ENOMEM);
  946. cell = nvmem_cell_get(dev, id);
  947. if (!IS_ERR(cell)) {
  948. *ptr = cell;
  949. devres_add(dev, ptr);
  950. } else {
  951. devres_free(ptr);
  952. }
  953. return cell;
  954. }
  955. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  956. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  957. {
  958. struct nvmem_cell **c = res;
  959. if (WARN_ON(!c || !*c))
  960. return 0;
  961. return *c == data;
  962. }
  963. /**
  964. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  965. * from devm_nvmem_cell_get.
  966. *
  967. * @dev: Device that requests the nvmem cell.
  968. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
  969. */
  970. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  971. {
  972. int ret;
  973. ret = devres_release(dev, devm_nvmem_cell_release,
  974. devm_nvmem_cell_match, cell);
  975. WARN_ON(ret);
  976. }
  977. EXPORT_SYMBOL(devm_nvmem_cell_put);
  978. /**
  979. * nvmem_cell_put() - Release previously allocated nvmem cell.
  980. *
  981. * @cell: Previously allocated nvmem cell by nvmem_cell_get().
  982. */
  983. void nvmem_cell_put(struct nvmem_cell *cell)
  984. {
  985. struct nvmem_device *nvmem = cell->nvmem;
  986. __nvmem_device_put(nvmem);
  987. }
  988. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  989. static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
  990. {
  991. u8 *p, *b;
  992. int i, extra, bit_offset = cell->bit_offset;
  993. p = b = buf;
  994. if (bit_offset) {
  995. /* First shift */
  996. *b++ >>= bit_offset;
  997. /* setup rest of the bytes if any */
  998. for (i = 1; i < cell->bytes; i++) {
  999. /* Get bits from next byte and shift them towards msb */
  1000. *p |= *b << (BITS_PER_BYTE - bit_offset);
  1001. p = b;
  1002. *b++ >>= bit_offset;
  1003. }
  1004. } else {
  1005. /* point to the msb */
  1006. p += cell->bytes - 1;
  1007. }
  1008. /* result fits in less bytes */
  1009. extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
  1010. while (--extra >= 0)
  1011. *p-- = 0;
  1012. /* clear msb bits if any leftover in the last byte */
  1013. if (cell->nbits % BITS_PER_BYTE)
  1014. *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
  1015. }
  1016. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  1017. struct nvmem_cell *cell,
  1018. void *buf, size_t *len)
  1019. {
  1020. int rc;
  1021. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
  1022. if (rc)
  1023. return rc;
  1024. /* shift bits in-place */
  1025. if (cell->bit_offset || cell->nbits)
  1026. nvmem_shift_read_buffer_in_place(cell, buf);
  1027. if (len)
  1028. *len = cell->bytes;
  1029. return 0;
  1030. }
  1031. /**
  1032. * nvmem_cell_read() - Read a given nvmem cell
  1033. *
  1034. * @cell: nvmem cell to be read.
  1035. * @len: pointer to length of cell which will be populated on successful read;
  1036. * can be NULL.
  1037. *
  1038. * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
  1039. * buffer should be freed by the consumer with a kfree().
  1040. */
  1041. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  1042. {
  1043. struct nvmem_device *nvmem = cell->nvmem;
  1044. u8 *buf;
  1045. int rc;
  1046. if (!nvmem)
  1047. return ERR_PTR(-EINVAL);
  1048. buf = kzalloc(cell->bytes, GFP_KERNEL);
  1049. if (!buf)
  1050. return ERR_PTR(-ENOMEM);
  1051. rc = __nvmem_cell_read(nvmem, cell, buf, len);
  1052. if (rc) {
  1053. kfree(buf);
  1054. return ERR_PTR(rc);
  1055. }
  1056. return buf;
  1057. }
  1058. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  1059. static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
  1060. u8 *_buf, int len)
  1061. {
  1062. struct nvmem_device *nvmem = cell->nvmem;
  1063. int i, rc, nbits, bit_offset = cell->bit_offset;
  1064. u8 v, *p, *buf, *b, pbyte, pbits;
  1065. nbits = cell->nbits;
  1066. buf = kzalloc(cell->bytes, GFP_KERNEL);
  1067. if (!buf)
  1068. return ERR_PTR(-ENOMEM);
  1069. memcpy(buf, _buf, len);
  1070. p = b = buf;
  1071. if (bit_offset) {
  1072. pbyte = *b;
  1073. *b <<= bit_offset;
  1074. /* setup the first byte with lsb bits from nvmem */
  1075. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  1076. if (rc)
  1077. goto err;
  1078. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  1079. /* setup rest of the byte if any */
  1080. for (i = 1; i < cell->bytes; i++) {
  1081. /* Get last byte bits and shift them towards lsb */
  1082. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  1083. pbyte = *b;
  1084. p = b;
  1085. *b <<= bit_offset;
  1086. *b++ |= pbits;
  1087. }
  1088. }
  1089. /* if it's not end on byte boundary */
  1090. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  1091. /* setup the last byte with msb bits from nvmem */
  1092. rc = nvmem_reg_read(nvmem,
  1093. cell->offset + cell->bytes - 1, &v, 1);
  1094. if (rc)
  1095. goto err;
  1096. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  1097. }
  1098. return buf;
  1099. err:
  1100. kfree(buf);
  1101. return ERR_PTR(rc);
  1102. }
  1103. /**
  1104. * nvmem_cell_write() - Write to a given nvmem cell
  1105. *
  1106. * @cell: nvmem cell to be written.
  1107. * @buf: Buffer to be written.
  1108. * @len: length of buffer to be written to nvmem cell.
  1109. *
  1110. * Return: length of bytes written or negative on failure.
  1111. */
  1112. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  1113. {
  1114. struct nvmem_device *nvmem = cell->nvmem;
  1115. int rc;
  1116. if (!nvmem || nvmem->read_only ||
  1117. (cell->bit_offset == 0 && len != cell->bytes))
  1118. return -EINVAL;
  1119. if (cell->bit_offset || cell->nbits) {
  1120. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  1121. if (IS_ERR(buf))
  1122. return PTR_ERR(buf);
  1123. }
  1124. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  1125. /* free the tmp buffer */
  1126. if (cell->bit_offset || cell->nbits)
  1127. kfree(buf);
  1128. if (rc)
  1129. return rc;
  1130. return len;
  1131. }
  1132. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  1133. static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
  1134. void *val, size_t count)
  1135. {
  1136. struct nvmem_cell *cell;
  1137. void *buf;
  1138. size_t len;
  1139. cell = nvmem_cell_get(dev, cell_id);
  1140. if (IS_ERR(cell))
  1141. return PTR_ERR(cell);
  1142. buf = nvmem_cell_read(cell, &len);
  1143. if (IS_ERR(buf)) {
  1144. nvmem_cell_put(cell);
  1145. return PTR_ERR(buf);
  1146. }
  1147. if (len != count) {
  1148. kfree(buf);
  1149. nvmem_cell_put(cell);
  1150. return -EINVAL;
  1151. }
  1152. memcpy(val, buf, count);
  1153. kfree(buf);
  1154. nvmem_cell_put(cell);
  1155. return 0;
  1156. }
  1157. /**
  1158. * nvmem_cell_read_u8() - Read a cell value as a u8
  1159. *
  1160. * @dev: Device that requests the nvmem cell.
  1161. * @cell_id: Name of nvmem cell to read.
  1162. * @val: pointer to output value.
  1163. *
  1164. * Return: 0 on success or negative errno.
  1165. */
  1166. int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
  1167. {
  1168. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1169. }
  1170. EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
  1171. /**
  1172. * nvmem_cell_read_u16() - Read a cell value as a u16
  1173. *
  1174. * @dev: Device that requests the nvmem cell.
  1175. * @cell_id: Name of nvmem cell to read.
  1176. * @val: pointer to output value.
  1177. *
  1178. * Return: 0 on success or negative errno.
  1179. */
  1180. int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
  1181. {
  1182. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1183. }
  1184. EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
  1185. /**
  1186. * nvmem_cell_read_u32() - Read a cell value as a u32
  1187. *
  1188. * @dev: Device that requests the nvmem cell.
  1189. * @cell_id: Name of nvmem cell to read.
  1190. * @val: pointer to output value.
  1191. *
  1192. * Return: 0 on success or negative errno.
  1193. */
  1194. int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
  1195. {
  1196. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1197. }
  1198. EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
  1199. /**
  1200. * nvmem_cell_read_u64() - Read a cell value as a u64
  1201. *
  1202. * @dev: Device that requests the nvmem cell.
  1203. * @cell_id: Name of nvmem cell to read.
  1204. * @val: pointer to output value.
  1205. *
  1206. * Return: 0 on success or negative errno.
  1207. */
  1208. int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
  1209. {
  1210. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1211. }
  1212. EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
  1213. /**
  1214. * nvmem_device_cell_read() - Read a given nvmem device and cell
  1215. *
  1216. * @nvmem: nvmem device to read from.
  1217. * @info: nvmem cell info to be read.
  1218. * @buf: buffer pointer which will be populated on successful read.
  1219. *
  1220. * Return: length of successful bytes read on success and negative
  1221. * error code on error.
  1222. */
  1223. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  1224. struct nvmem_cell_info *info, void *buf)
  1225. {
  1226. struct nvmem_cell cell;
  1227. int rc;
  1228. ssize_t len;
  1229. if (!nvmem)
  1230. return -EINVAL;
  1231. rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
  1232. if (rc)
  1233. return rc;
  1234. rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
  1235. if (rc)
  1236. return rc;
  1237. return len;
  1238. }
  1239. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  1240. /**
  1241. * nvmem_device_cell_write() - Write cell to a given nvmem device
  1242. *
  1243. * @nvmem: nvmem device to be written to.
  1244. * @info: nvmem cell info to be written.
  1245. * @buf: buffer to be written to cell.
  1246. *
  1247. * Return: length of bytes written or negative error code on failure.
  1248. */
  1249. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  1250. struct nvmem_cell_info *info, void *buf)
  1251. {
  1252. struct nvmem_cell cell;
  1253. int rc;
  1254. if (!nvmem)
  1255. return -EINVAL;
  1256. rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
  1257. if (rc)
  1258. return rc;
  1259. return nvmem_cell_write(&cell, buf, cell.bytes);
  1260. }
  1261. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  1262. /**
  1263. * nvmem_device_read() - Read from a given nvmem device
  1264. *
  1265. * @nvmem: nvmem device to read from.
  1266. * @offset: offset in nvmem device.
  1267. * @bytes: number of bytes to read.
  1268. * @buf: buffer pointer which will be populated on successful read.
  1269. *
  1270. * Return: length of successful bytes read on success and negative
  1271. * error code on error.
  1272. */
  1273. int nvmem_device_read(struct nvmem_device *nvmem,
  1274. unsigned int offset,
  1275. size_t bytes, void *buf)
  1276. {
  1277. int rc;
  1278. if (!nvmem)
  1279. return -EINVAL;
  1280. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  1281. if (rc)
  1282. return rc;
  1283. return bytes;
  1284. }
  1285. EXPORT_SYMBOL_GPL(nvmem_device_read);
  1286. /**
  1287. * nvmem_device_write() - Write cell to a given nvmem device
  1288. *
  1289. * @nvmem: nvmem device to be written to.
  1290. * @offset: offset in nvmem device.
  1291. * @bytes: number of bytes to write.
  1292. * @buf: buffer to be written.
  1293. *
  1294. * Return: length of bytes written or negative error code on failure.
  1295. */
  1296. int nvmem_device_write(struct nvmem_device *nvmem,
  1297. unsigned int offset,
  1298. size_t bytes, void *buf)
  1299. {
  1300. int rc;
  1301. if (!nvmem)
  1302. return -EINVAL;
  1303. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  1304. if (rc)
  1305. return rc;
  1306. return bytes;
  1307. }
  1308. EXPORT_SYMBOL_GPL(nvmem_device_write);
  1309. /**
  1310. * nvmem_add_cell_table() - register a table of cell info entries
  1311. *
  1312. * @table: table of cell info entries
  1313. */
  1314. void nvmem_add_cell_table(struct nvmem_cell_table *table)
  1315. {
  1316. mutex_lock(&nvmem_cell_mutex);
  1317. list_add_tail(&table->node, &nvmem_cell_tables);
  1318. mutex_unlock(&nvmem_cell_mutex);
  1319. }
  1320. EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
  1321. /**
  1322. * nvmem_del_cell_table() - remove a previously registered cell info table
  1323. *
  1324. * @table: table of cell info entries
  1325. */
  1326. void nvmem_del_cell_table(struct nvmem_cell_table *table)
  1327. {
  1328. mutex_lock(&nvmem_cell_mutex);
  1329. list_del(&table->node);
  1330. mutex_unlock(&nvmem_cell_mutex);
  1331. }
  1332. EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
  1333. /**
  1334. * nvmem_add_cell_lookups() - register a list of cell lookup entries
  1335. *
  1336. * @entries: array of cell lookup entries
  1337. * @nentries: number of cell lookup entries in the array
  1338. */
  1339. void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
  1340. {
  1341. int i;
  1342. mutex_lock(&nvmem_lookup_mutex);
  1343. for (i = 0; i < nentries; i++)
  1344. list_add_tail(&entries[i].node, &nvmem_lookup_list);
  1345. mutex_unlock(&nvmem_lookup_mutex);
  1346. }
  1347. EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
  1348. /**
  1349. * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
  1350. * entries
  1351. *
  1352. * @entries: array of cell lookup entries
  1353. * @nentries: number of cell lookup entries in the array
  1354. */
  1355. void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
  1356. {
  1357. int i;
  1358. mutex_lock(&nvmem_lookup_mutex);
  1359. for (i = 0; i < nentries; i++)
  1360. list_del(&entries[i].node);
  1361. mutex_unlock(&nvmem_lookup_mutex);
  1362. }
  1363. EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
  1364. /**
  1365. * nvmem_dev_name() - Get the name of a given nvmem device.
  1366. *
  1367. * @nvmem: nvmem device.
  1368. *
  1369. * Return: name of the nvmem device.
  1370. */
  1371. const char *nvmem_dev_name(struct nvmem_device *nvmem)
  1372. {
  1373. return dev_name(&nvmem->dev);
  1374. }
  1375. EXPORT_SYMBOL_GPL(nvmem_dev_name);
  1376. static int __init nvmem_init(void)
  1377. {
  1378. return bus_register(&nvmem_bus_type);
  1379. }
  1380. static void __exit nvmem_exit(void)
  1381. {
  1382. bus_unregister(&nvmem_bus_type);
  1383. }
  1384. subsys_initcall(nvmem_init);
  1385. module_exit(nvmem_exit);
  1386. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  1387. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  1388. MODULE_DESCRIPTION("nvmem Driver Core");
  1389. MODULE_LICENSE("GPL v2");