core.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  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. #include "internals.h"
  21. #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  22. #define FLAG_COMPAT BIT(0)
  23. struct nvmem_cell_entry {
  24. const char *name;
  25. int offset;
  26. size_t raw_len;
  27. int bytes;
  28. int bit_offset;
  29. int nbits;
  30. nvmem_cell_post_process_t read_post_process;
  31. void *priv;
  32. struct device_node *np;
  33. struct nvmem_device *nvmem;
  34. struct list_head node;
  35. };
  36. struct nvmem_cell {
  37. struct nvmem_cell_entry *entry;
  38. const char *id;
  39. int index;
  40. };
  41. static DEFINE_MUTEX(nvmem_mutex);
  42. static DEFINE_IDA(nvmem_ida);
  43. static DEFINE_MUTEX(nvmem_cell_mutex);
  44. static LIST_HEAD(nvmem_cell_tables);
  45. static DEFINE_MUTEX(nvmem_lookup_mutex);
  46. static LIST_HEAD(nvmem_lookup_list);
  47. static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
  48. static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  49. void *val, size_t bytes)
  50. {
  51. if (nvmem->reg_read)
  52. return nvmem->reg_read(nvmem->priv, offset, val, bytes);
  53. return -EINVAL;
  54. }
  55. static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  56. void *val, size_t bytes)
  57. {
  58. int ret;
  59. if (nvmem->reg_write) {
  60. gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
  61. ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
  62. gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
  63. return ret;
  64. }
  65. return -EINVAL;
  66. }
  67. static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
  68. unsigned int offset, void *val,
  69. size_t bytes, int write)
  70. {
  71. unsigned int end = offset + bytes;
  72. unsigned int kend, ksize;
  73. const struct nvmem_keepout *keepout = nvmem->keepout;
  74. const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
  75. int rc;
  76. /*
  77. * Skip all keepouts before the range being accessed.
  78. * Keepouts are sorted.
  79. */
  80. while ((keepout < keepoutend) && (keepout->end <= offset))
  81. keepout++;
  82. while ((offset < end) && (keepout < keepoutend)) {
  83. /* Access the valid portion before the keepout. */
  84. if (offset < keepout->start) {
  85. kend = min(end, keepout->start);
  86. ksize = kend - offset;
  87. if (write)
  88. rc = __nvmem_reg_write(nvmem, offset, val, ksize);
  89. else
  90. rc = __nvmem_reg_read(nvmem, offset, val, ksize);
  91. if (rc)
  92. return rc;
  93. offset += ksize;
  94. val += ksize;
  95. }
  96. /*
  97. * Now we're aligned to the start of this keepout zone. Go
  98. * through it.
  99. */
  100. kend = min(end, keepout->end);
  101. ksize = kend - offset;
  102. if (!write)
  103. memset(val, keepout->value, ksize);
  104. val += ksize;
  105. offset += ksize;
  106. keepout++;
  107. }
  108. /*
  109. * If we ran out of keepouts but there's still stuff to do, send it
  110. * down directly
  111. */
  112. if (offset < end) {
  113. ksize = end - offset;
  114. if (write)
  115. return __nvmem_reg_write(nvmem, offset, val, ksize);
  116. else
  117. return __nvmem_reg_read(nvmem, offset, val, ksize);
  118. }
  119. return 0;
  120. }
  121. static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  122. void *val, size_t bytes)
  123. {
  124. if (!nvmem->nkeepout)
  125. return __nvmem_reg_read(nvmem, offset, val, bytes);
  126. return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
  127. }
  128. static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  129. void *val, size_t bytes)
  130. {
  131. if (!nvmem->nkeepout)
  132. return __nvmem_reg_write(nvmem, offset, val, bytes);
  133. return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
  134. }
  135. #ifdef CONFIG_NVMEM_SYSFS
  136. static const char * const nvmem_type_str[] = {
  137. [NVMEM_TYPE_UNKNOWN] = "Unknown",
  138. [NVMEM_TYPE_EEPROM] = "EEPROM",
  139. [NVMEM_TYPE_OTP] = "OTP",
  140. [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
  141. [NVMEM_TYPE_FRAM] = "FRAM",
  142. };
  143. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  144. static struct lock_class_key eeprom_lock_key;
  145. #endif
  146. static ssize_t type_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct nvmem_device *nvmem = to_nvmem_device(dev);
  150. return sysfs_emit(buf, "%s\n", nvmem_type_str[nvmem->type]);
  151. }
  152. static DEVICE_ATTR_RO(type);
  153. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  154. char *buf)
  155. {
  156. struct nvmem_device *nvmem = to_nvmem_device(dev);
  157. return sysfs_emit(buf, "%d\n", nvmem->read_only);
  158. }
  159. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct nvmem_device *nvmem = to_nvmem_device(dev);
  163. int ret = kstrtobool(buf, &nvmem->read_only);
  164. if (ret < 0)
  165. return ret;
  166. return count;
  167. }
  168. static DEVICE_ATTR_RW(force_ro);
  169. static struct attribute *nvmem_attrs[] = {
  170. &dev_attr_force_ro.attr,
  171. &dev_attr_type.attr,
  172. NULL,
  173. };
  174. static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
  175. struct bin_attribute *attr, char *buf,
  176. loff_t pos, size_t count)
  177. {
  178. struct device *dev;
  179. struct nvmem_device *nvmem;
  180. int rc;
  181. if (attr->private)
  182. dev = attr->private;
  183. else
  184. dev = kobj_to_dev(kobj);
  185. nvmem = to_nvmem_device(dev);
  186. if (!IS_ALIGNED(pos, nvmem->stride))
  187. return -EINVAL;
  188. if (count < nvmem->word_size)
  189. return -EINVAL;
  190. count = round_down(count, nvmem->word_size);
  191. if (!nvmem->reg_read)
  192. return -EPERM;
  193. rc = nvmem_reg_read(nvmem, pos, buf, count);
  194. if (rc)
  195. return rc;
  196. return count;
  197. }
  198. static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
  199. struct bin_attribute *attr, char *buf,
  200. loff_t pos, size_t count)
  201. {
  202. struct device *dev;
  203. struct nvmem_device *nvmem;
  204. int rc;
  205. if (attr->private)
  206. dev = attr->private;
  207. else
  208. dev = kobj_to_dev(kobj);
  209. nvmem = to_nvmem_device(dev);
  210. if (!IS_ALIGNED(pos, nvmem->stride))
  211. return -EINVAL;
  212. if (count < nvmem->word_size)
  213. return -EINVAL;
  214. count = round_down(count, nvmem->word_size);
  215. if (!nvmem->reg_write || nvmem->read_only)
  216. return -EPERM;
  217. rc = nvmem_reg_write(nvmem, pos, buf, count);
  218. if (rc)
  219. return rc;
  220. return count;
  221. }
  222. static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
  223. {
  224. umode_t mode = 0400;
  225. if (!nvmem->root_only)
  226. mode |= 0044;
  227. if (!nvmem->read_only)
  228. mode |= 0200;
  229. if (!nvmem->reg_write)
  230. mode &= ~0200;
  231. if (!nvmem->reg_read)
  232. mode &= ~0444;
  233. return mode;
  234. }
  235. static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
  236. struct bin_attribute *attr, int i)
  237. {
  238. struct device *dev = kobj_to_dev(kobj);
  239. struct nvmem_device *nvmem = to_nvmem_device(dev);
  240. attr->size = nvmem->size;
  241. return nvmem_bin_attr_get_umode(nvmem);
  242. }
  243. static umode_t nvmem_attr_is_visible(struct kobject *kobj,
  244. struct attribute *attr, int i)
  245. {
  246. struct device *dev = kobj_to_dev(kobj);
  247. struct nvmem_device *nvmem = to_nvmem_device(dev);
  248. /*
  249. * If the device has no .reg_write operation, do not allow
  250. * configuration as read-write.
  251. * If the device is set as read-only by configuration, it
  252. * can be forced into read-write mode using the 'force_ro'
  253. * attribute.
  254. */
  255. if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
  256. return 0; /* Attribute not visible */
  257. return attr->mode;
  258. }
  259. static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
  260. const char *id, int index);
  261. static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
  262. struct bin_attribute *attr, char *buf,
  263. loff_t pos, size_t count)
  264. {
  265. struct nvmem_cell_entry *entry;
  266. struct nvmem_cell *cell = NULL;
  267. size_t cell_sz, read_len;
  268. void *content;
  269. entry = attr->private;
  270. cell = nvmem_create_cell(entry, entry->name, 0);
  271. if (IS_ERR(cell))
  272. return PTR_ERR(cell);
  273. if (!cell)
  274. return -EINVAL;
  275. content = nvmem_cell_read(cell, &cell_sz);
  276. if (IS_ERR(content)) {
  277. read_len = PTR_ERR(content);
  278. goto destroy_cell;
  279. }
  280. read_len = min_t(unsigned int, cell_sz - pos, count);
  281. memcpy(buf, content + pos, read_len);
  282. kfree(content);
  283. destroy_cell:
  284. kfree_const(cell->id);
  285. kfree(cell);
  286. return read_len;
  287. }
  288. /* default read/write permissions */
  289. static struct bin_attribute bin_attr_rw_nvmem = {
  290. .attr = {
  291. .name = "nvmem",
  292. .mode = 0644,
  293. },
  294. .read = bin_attr_nvmem_read,
  295. .write = bin_attr_nvmem_write,
  296. };
  297. static struct bin_attribute *nvmem_bin_attributes[] = {
  298. &bin_attr_rw_nvmem,
  299. NULL,
  300. };
  301. static const struct attribute_group nvmem_bin_group = {
  302. .bin_attrs = nvmem_bin_attributes,
  303. .attrs = nvmem_attrs,
  304. .is_bin_visible = nvmem_bin_attr_is_visible,
  305. .is_visible = nvmem_attr_is_visible,
  306. };
  307. static const struct attribute_group *nvmem_dev_groups[] = {
  308. &nvmem_bin_group,
  309. NULL,
  310. };
  311. static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
  312. .attr = {
  313. .name = "eeprom",
  314. },
  315. .read = bin_attr_nvmem_read,
  316. .write = bin_attr_nvmem_write,
  317. };
  318. /*
  319. * nvmem_setup_compat() - Create an additional binary entry in
  320. * drivers sys directory, to be backwards compatible with the older
  321. * drivers/misc/eeprom drivers.
  322. */
  323. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  324. const struct nvmem_config *config)
  325. {
  326. int rval;
  327. if (!config->compat)
  328. return 0;
  329. if (!config->base_dev)
  330. return -EINVAL;
  331. nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
  332. if (config->type == NVMEM_TYPE_FRAM)
  333. nvmem->eeprom.attr.name = "fram";
  334. nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
  335. nvmem->eeprom.size = nvmem->size;
  336. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  337. nvmem->eeprom.attr.key = &eeprom_lock_key;
  338. #endif
  339. nvmem->eeprom.private = &nvmem->dev;
  340. nvmem->base_dev = config->base_dev;
  341. rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
  342. if (rval) {
  343. dev_err(&nvmem->dev,
  344. "Failed to create eeprom binary file %d\n", rval);
  345. return rval;
  346. }
  347. nvmem->flags |= FLAG_COMPAT;
  348. return 0;
  349. }
  350. static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
  351. const struct nvmem_config *config)
  352. {
  353. if (config->compat)
  354. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  355. }
  356. static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
  357. {
  358. struct attribute_group group = {
  359. .name = "cells",
  360. };
  361. struct nvmem_cell_entry *entry;
  362. struct bin_attribute *attrs;
  363. unsigned int ncells = 0, i = 0;
  364. int ret = 0;
  365. mutex_lock(&nvmem_mutex);
  366. if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
  367. goto unlock_mutex;
  368. /* Allocate an array of attributes with a sentinel */
  369. ncells = list_count_nodes(&nvmem->cells);
  370. group.bin_attrs = devm_kcalloc(&nvmem->dev, ncells + 1,
  371. sizeof(struct bin_attribute *), GFP_KERNEL);
  372. if (!group.bin_attrs) {
  373. ret = -ENOMEM;
  374. goto unlock_mutex;
  375. }
  376. attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
  377. if (!attrs) {
  378. ret = -ENOMEM;
  379. goto unlock_mutex;
  380. }
  381. /* Initialize each attribute to take the name and size of the cell */
  382. list_for_each_entry(entry, &nvmem->cells, node) {
  383. sysfs_bin_attr_init(&attrs[i]);
  384. attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
  385. "%s@%x,%x", entry->name,
  386. entry->offset,
  387. entry->bit_offset);
  388. attrs[i].attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem);
  389. attrs[i].size = entry->bytes;
  390. attrs[i].read = &nvmem_cell_attr_read;
  391. attrs[i].private = entry;
  392. if (!attrs[i].attr.name) {
  393. ret = -ENOMEM;
  394. goto unlock_mutex;
  395. }
  396. group.bin_attrs[i] = &attrs[i];
  397. i++;
  398. }
  399. ret = device_add_group(&nvmem->dev, &group);
  400. if (ret)
  401. goto unlock_mutex;
  402. nvmem->sysfs_cells_populated = true;
  403. unlock_mutex:
  404. mutex_unlock(&nvmem_mutex);
  405. return ret;
  406. }
  407. #else /* CONFIG_NVMEM_SYSFS */
  408. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  409. const struct nvmem_config *config)
  410. {
  411. return -ENOSYS;
  412. }
  413. static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
  414. const struct nvmem_config *config)
  415. {
  416. }
  417. #endif /* CONFIG_NVMEM_SYSFS */
  418. static void nvmem_release(struct device *dev)
  419. {
  420. struct nvmem_device *nvmem = to_nvmem_device(dev);
  421. ida_free(&nvmem_ida, nvmem->id);
  422. gpiod_put(nvmem->wp_gpio);
  423. kfree(nvmem);
  424. }
  425. static const struct device_type nvmem_provider_type = {
  426. .release = nvmem_release,
  427. };
  428. static struct bus_type nvmem_bus_type = {
  429. .name = "nvmem",
  430. };
  431. static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
  432. {
  433. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
  434. mutex_lock(&nvmem_mutex);
  435. list_del(&cell->node);
  436. mutex_unlock(&nvmem_mutex);
  437. of_node_put(cell->np);
  438. kfree_const(cell->name);
  439. kfree(cell);
  440. }
  441. static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
  442. {
  443. struct nvmem_cell_entry *cell, *p;
  444. list_for_each_entry_safe(cell, p, &nvmem->cells, node)
  445. nvmem_cell_entry_drop(cell);
  446. }
  447. static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
  448. {
  449. mutex_lock(&nvmem_mutex);
  450. list_add_tail(&cell->node, &cell->nvmem->cells);
  451. mutex_unlock(&nvmem_mutex);
  452. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
  453. }
  454. static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
  455. const struct nvmem_cell_info *info,
  456. struct nvmem_cell_entry *cell)
  457. {
  458. cell->nvmem = nvmem;
  459. cell->offset = info->offset;
  460. cell->raw_len = info->raw_len ?: info->bytes;
  461. cell->bytes = info->bytes;
  462. cell->name = info->name;
  463. cell->read_post_process = info->read_post_process;
  464. cell->priv = info->priv;
  465. cell->bit_offset = info->bit_offset;
  466. cell->nbits = info->nbits;
  467. cell->np = info->np;
  468. if (cell->nbits) {
  469. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  470. BITS_PER_BYTE);
  471. cell->raw_len = ALIGN(cell->bytes, nvmem->word_size);
  472. }
  473. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  474. dev_err(&nvmem->dev,
  475. "cell %s unaligned to nvmem stride %d\n",
  476. cell->name ?: "<unknown>", nvmem->stride);
  477. return -EINVAL;
  478. }
  479. if (!IS_ALIGNED(cell->raw_len, nvmem->word_size)) {
  480. dev_err(&nvmem->dev,
  481. "cell %s raw len %zd unaligned to nvmem word size %d\n",
  482. cell->name ?: "<unknown>", cell->raw_len,
  483. nvmem->word_size);
  484. if (info->raw_len)
  485. return -EINVAL;
  486. cell->raw_len = ALIGN(cell->raw_len, nvmem->word_size);
  487. }
  488. return 0;
  489. }
  490. static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
  491. const struct nvmem_cell_info *info,
  492. struct nvmem_cell_entry *cell)
  493. {
  494. int err;
  495. err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
  496. if (err)
  497. return err;
  498. cell->name = kstrdup_const(info->name, GFP_KERNEL);
  499. if (!cell->name)
  500. return -ENOMEM;
  501. return 0;
  502. }
  503. /**
  504. * nvmem_add_one_cell() - Add one cell information to an nvmem device
  505. *
  506. * @nvmem: nvmem device to add cells to.
  507. * @info: nvmem cell info to add to the device
  508. *
  509. * Return: 0 or negative error code on failure.
  510. */
  511. int nvmem_add_one_cell(struct nvmem_device *nvmem,
  512. const struct nvmem_cell_info *info)
  513. {
  514. struct nvmem_cell_entry *cell;
  515. int rval;
  516. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  517. if (!cell)
  518. return -ENOMEM;
  519. rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
  520. if (rval) {
  521. kfree(cell);
  522. return rval;
  523. }
  524. nvmem_cell_entry_add(cell);
  525. return 0;
  526. }
  527. EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
  528. /**
  529. * nvmem_add_cells() - Add cell information to an nvmem device
  530. *
  531. * @nvmem: nvmem device to add cells to.
  532. * @info: nvmem cell info to add to the device
  533. * @ncells: number of cells in info
  534. *
  535. * Return: 0 or negative error code on failure.
  536. */
  537. static int nvmem_add_cells(struct nvmem_device *nvmem,
  538. const struct nvmem_cell_info *info,
  539. int ncells)
  540. {
  541. int i, rval;
  542. for (i = 0; i < ncells; i++) {
  543. rval = nvmem_add_one_cell(nvmem, &info[i]);
  544. if (rval)
  545. return rval;
  546. }
  547. return 0;
  548. }
  549. /**
  550. * nvmem_register_notifier() - Register a notifier block for nvmem events.
  551. *
  552. * @nb: notifier block to be called on nvmem events.
  553. *
  554. * Return: 0 on success, negative error number on failure.
  555. */
  556. int nvmem_register_notifier(struct notifier_block *nb)
  557. {
  558. return blocking_notifier_chain_register(&nvmem_notifier, nb);
  559. }
  560. EXPORT_SYMBOL_GPL(nvmem_register_notifier);
  561. /**
  562. * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
  563. *
  564. * @nb: notifier block to be unregistered.
  565. *
  566. * Return: 0 on success, negative error number on failure.
  567. */
  568. int nvmem_unregister_notifier(struct notifier_block *nb)
  569. {
  570. return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
  571. }
  572. EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
  573. static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
  574. {
  575. const struct nvmem_cell_info *info;
  576. struct nvmem_cell_table *table;
  577. struct nvmem_cell_entry *cell;
  578. int rval = 0, i;
  579. mutex_lock(&nvmem_cell_mutex);
  580. list_for_each_entry(table, &nvmem_cell_tables, node) {
  581. if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
  582. for (i = 0; i < table->ncells; i++) {
  583. info = &table->cells[i];
  584. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  585. if (!cell) {
  586. rval = -ENOMEM;
  587. goto out;
  588. }
  589. rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
  590. if (rval) {
  591. kfree(cell);
  592. goto out;
  593. }
  594. nvmem_cell_entry_add(cell);
  595. }
  596. }
  597. }
  598. out:
  599. mutex_unlock(&nvmem_cell_mutex);
  600. return rval;
  601. }
  602. static struct nvmem_cell_entry *
  603. nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
  604. {
  605. struct nvmem_cell_entry *iter, *cell = NULL;
  606. mutex_lock(&nvmem_mutex);
  607. list_for_each_entry(iter, &nvmem->cells, node) {
  608. if (strcmp(cell_id, iter->name) == 0) {
  609. cell = iter;
  610. break;
  611. }
  612. }
  613. mutex_unlock(&nvmem_mutex);
  614. return cell;
  615. }
  616. static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
  617. {
  618. unsigned int cur = 0;
  619. const struct nvmem_keepout *keepout = nvmem->keepout;
  620. const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
  621. while (keepout < keepoutend) {
  622. /* Ensure keepouts are sorted and don't overlap. */
  623. if (keepout->start < cur) {
  624. dev_err(&nvmem->dev,
  625. "Keepout regions aren't sorted or overlap.\n");
  626. return -ERANGE;
  627. }
  628. if (keepout->end < keepout->start) {
  629. dev_err(&nvmem->dev,
  630. "Invalid keepout region.\n");
  631. return -EINVAL;
  632. }
  633. /*
  634. * Validate keepouts (and holes between) don't violate
  635. * word_size constraints.
  636. */
  637. if ((keepout->end - keepout->start < nvmem->word_size) ||
  638. ((keepout->start != cur) &&
  639. (keepout->start - cur < nvmem->word_size))) {
  640. dev_err(&nvmem->dev,
  641. "Keepout regions violate word_size constraints.\n");
  642. return -ERANGE;
  643. }
  644. /* Validate keepouts don't violate stride (alignment). */
  645. if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
  646. !IS_ALIGNED(keepout->end, nvmem->stride)) {
  647. dev_err(&nvmem->dev,
  648. "Keepout regions violate stride.\n");
  649. return -EINVAL;
  650. }
  651. cur = keepout->end;
  652. keepout++;
  653. }
  654. return 0;
  655. }
  656. static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
  657. {
  658. struct device *dev = &nvmem->dev;
  659. struct device_node *child;
  660. const __be32 *addr;
  661. int len, ret;
  662. for_each_child_of_node(np, child) {
  663. struct nvmem_cell_info info = {0};
  664. addr = of_get_property(child, "reg", &len);
  665. if (!addr)
  666. continue;
  667. if (len < 2 * sizeof(u32)) {
  668. dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
  669. of_node_put(child);
  670. return -EINVAL;
  671. }
  672. info.offset = be32_to_cpup(addr++);
  673. info.bytes = be32_to_cpup(addr);
  674. info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
  675. addr = of_get_property(child, "bits", &len);
  676. if (addr && len == (2 * sizeof(u32))) {
  677. info.bit_offset = be32_to_cpup(addr++);
  678. info.nbits = be32_to_cpup(addr);
  679. if (info.bit_offset >= BITS_PER_BYTE * info.bytes ||
  680. info.nbits < 1 ||
  681. info.bit_offset + info.nbits > BITS_PER_BYTE * info.bytes) {
  682. dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
  683. of_node_put(child);
  684. return -EINVAL;
  685. }
  686. }
  687. info.np = of_node_get(child);
  688. if (nvmem->fixup_dt_cell_info)
  689. nvmem->fixup_dt_cell_info(nvmem, &info);
  690. ret = nvmem_add_one_cell(nvmem, &info);
  691. kfree(info.name);
  692. if (ret) {
  693. of_node_put(child);
  694. return ret;
  695. }
  696. }
  697. return 0;
  698. }
  699. static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
  700. {
  701. return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
  702. }
  703. static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem)
  704. {
  705. struct device_node *layout_np;
  706. int err = 0;
  707. layout_np = of_nvmem_layout_get_container(nvmem);
  708. if (!layout_np)
  709. return 0;
  710. if (of_device_is_compatible(layout_np, "fixed-layout"))
  711. err = nvmem_add_cells_from_dt(nvmem, layout_np);
  712. of_node_put(layout_np);
  713. return err;
  714. }
  715. int nvmem_layout_register(struct nvmem_layout *layout)
  716. {
  717. int ret;
  718. if (!layout->add_cells)
  719. return -EINVAL;
  720. /* Populate the cells */
  721. ret = layout->add_cells(layout);
  722. if (ret)
  723. return ret;
  724. #ifdef CONFIG_NVMEM_SYSFS
  725. ret = nvmem_populate_sysfs_cells(layout->nvmem);
  726. if (ret) {
  727. nvmem_device_remove_all_cells(layout->nvmem);
  728. return ret;
  729. }
  730. #endif
  731. return 0;
  732. }
  733. EXPORT_SYMBOL_GPL(nvmem_layout_register);
  734. void nvmem_layout_unregister(struct nvmem_layout *layout)
  735. {
  736. /* Keep the API even with an empty stub in case we need it later */
  737. }
  738. EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
  739. /**
  740. * nvmem_register() - Register a nvmem device for given nvmem_config.
  741. * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  742. *
  743. * @config: nvmem device configuration with which nvmem device is created.
  744. *
  745. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  746. * on success.
  747. */
  748. struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  749. {
  750. struct nvmem_device *nvmem;
  751. int rval;
  752. if (!config->dev)
  753. return ERR_PTR(-EINVAL);
  754. if (!config->reg_read && !config->reg_write)
  755. return ERR_PTR(-EINVAL);
  756. nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
  757. if (!nvmem)
  758. return ERR_PTR(-ENOMEM);
  759. rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
  760. if (rval < 0) {
  761. kfree(nvmem);
  762. return ERR_PTR(rval);
  763. }
  764. nvmem->id = rval;
  765. nvmem->dev.type = &nvmem_provider_type;
  766. nvmem->dev.bus = &nvmem_bus_type;
  767. nvmem->dev.parent = config->dev;
  768. device_initialize(&nvmem->dev);
  769. if (!config->ignore_wp)
  770. nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
  771. GPIOD_OUT_HIGH);
  772. if (IS_ERR(nvmem->wp_gpio)) {
  773. rval = PTR_ERR(nvmem->wp_gpio);
  774. nvmem->wp_gpio = NULL;
  775. goto err_put_device;
  776. }
  777. kref_init(&nvmem->refcnt);
  778. INIT_LIST_HEAD(&nvmem->cells);
  779. nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
  780. nvmem->owner = config->owner;
  781. if (!nvmem->owner && config->dev->driver)
  782. nvmem->owner = config->dev->driver->owner;
  783. nvmem->stride = config->stride ?: 1;
  784. nvmem->word_size = config->word_size ?: 1;
  785. nvmem->size = config->size;
  786. nvmem->root_only = config->root_only;
  787. nvmem->priv = config->priv;
  788. nvmem->type = config->type;
  789. nvmem->reg_read = config->reg_read;
  790. nvmem->reg_write = config->reg_write;
  791. nvmem->keepout = config->keepout;
  792. nvmem->nkeepout = config->nkeepout;
  793. if (config->of_node)
  794. nvmem->dev.of_node = config->of_node;
  795. else
  796. nvmem->dev.of_node = config->dev->of_node;
  797. switch (config->id) {
  798. case NVMEM_DEVID_NONE:
  799. rval = dev_set_name(&nvmem->dev, "%s", config->name);
  800. break;
  801. case NVMEM_DEVID_AUTO:
  802. rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
  803. break;
  804. default:
  805. rval = dev_set_name(&nvmem->dev, "%s%d",
  806. config->name ? : "nvmem",
  807. config->name ? config->id : nvmem->id);
  808. break;
  809. }
  810. if (rval)
  811. goto err_put_device;
  812. nvmem->read_only = device_property_present(config->dev, "read-only") ||
  813. config->read_only || !nvmem->reg_write;
  814. #ifdef CONFIG_NVMEM_SYSFS
  815. nvmem->dev.groups = nvmem_dev_groups;
  816. #endif
  817. if (nvmem->nkeepout) {
  818. rval = nvmem_validate_keepouts(nvmem);
  819. if (rval)
  820. goto err_put_device;
  821. }
  822. if (config->compat) {
  823. rval = nvmem_sysfs_setup_compat(nvmem, config);
  824. if (rval)
  825. goto err_put_device;
  826. }
  827. if (config->cells) {
  828. rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
  829. if (rval)
  830. goto err_remove_cells;
  831. }
  832. rval = nvmem_add_cells_from_table(nvmem);
  833. if (rval)
  834. goto err_remove_cells;
  835. if (config->add_legacy_fixed_of_cells) {
  836. rval = nvmem_add_cells_from_legacy_of(nvmem);
  837. if (rval)
  838. goto err_remove_cells;
  839. }
  840. rval = nvmem_add_cells_from_fixed_layout(nvmem);
  841. if (rval)
  842. goto err_remove_cells;
  843. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  844. rval = device_add(&nvmem->dev);
  845. if (rval)
  846. goto err_remove_cells;
  847. rval = nvmem_populate_layout(nvmem);
  848. if (rval)
  849. goto err_remove_dev;
  850. #ifdef CONFIG_NVMEM_SYSFS
  851. rval = nvmem_populate_sysfs_cells(nvmem);
  852. if (rval)
  853. goto err_destroy_layout;
  854. #endif
  855. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
  856. return nvmem;
  857. #ifdef CONFIG_NVMEM_SYSFS
  858. err_destroy_layout:
  859. nvmem_destroy_layout(nvmem);
  860. #endif
  861. err_remove_dev:
  862. device_del(&nvmem->dev);
  863. err_remove_cells:
  864. nvmem_device_remove_all_cells(nvmem);
  865. if (config->compat)
  866. nvmem_sysfs_remove_compat(nvmem, config);
  867. err_put_device:
  868. put_device(&nvmem->dev);
  869. return ERR_PTR(rval);
  870. }
  871. EXPORT_SYMBOL_GPL(nvmem_register);
  872. static void nvmem_device_release(struct kref *kref)
  873. {
  874. struct nvmem_device *nvmem;
  875. nvmem = container_of(kref, struct nvmem_device, refcnt);
  876. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
  877. if (nvmem->flags & FLAG_COMPAT)
  878. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  879. nvmem_device_remove_all_cells(nvmem);
  880. nvmem_destroy_layout(nvmem);
  881. device_unregister(&nvmem->dev);
  882. }
  883. /**
  884. * nvmem_unregister() - Unregister previously registered nvmem device
  885. *
  886. * @nvmem: Pointer to previously registered nvmem device.
  887. */
  888. void nvmem_unregister(struct nvmem_device *nvmem)
  889. {
  890. if (nvmem)
  891. kref_put(&nvmem->refcnt, nvmem_device_release);
  892. }
  893. EXPORT_SYMBOL_GPL(nvmem_unregister);
  894. static void devm_nvmem_unregister(void *nvmem)
  895. {
  896. nvmem_unregister(nvmem);
  897. }
  898. /**
  899. * devm_nvmem_register() - Register a managed nvmem device for given
  900. * nvmem_config.
  901. * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  902. *
  903. * @dev: Device that uses the nvmem device.
  904. * @config: nvmem device configuration with which nvmem device is created.
  905. *
  906. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  907. * on success.
  908. */
  909. struct nvmem_device *devm_nvmem_register(struct device *dev,
  910. const struct nvmem_config *config)
  911. {
  912. struct nvmem_device *nvmem;
  913. int ret;
  914. nvmem = nvmem_register(config);
  915. if (IS_ERR(nvmem))
  916. return nvmem;
  917. ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
  918. if (ret)
  919. return ERR_PTR(ret);
  920. return nvmem;
  921. }
  922. EXPORT_SYMBOL_GPL(devm_nvmem_register);
  923. static struct nvmem_device *__nvmem_device_get(void *data,
  924. int (*match)(struct device *dev, const void *data))
  925. {
  926. struct nvmem_device *nvmem = NULL;
  927. struct device *dev;
  928. mutex_lock(&nvmem_mutex);
  929. dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
  930. if (dev)
  931. nvmem = to_nvmem_device(dev);
  932. mutex_unlock(&nvmem_mutex);
  933. if (!nvmem)
  934. return ERR_PTR(-EPROBE_DEFER);
  935. if (!try_module_get(nvmem->owner)) {
  936. dev_err(&nvmem->dev,
  937. "could not increase module refcount for cell %s\n",
  938. nvmem_dev_name(nvmem));
  939. put_device(&nvmem->dev);
  940. return ERR_PTR(-EINVAL);
  941. }
  942. kref_get(&nvmem->refcnt);
  943. return nvmem;
  944. }
  945. static void __nvmem_device_put(struct nvmem_device *nvmem)
  946. {
  947. put_device(&nvmem->dev);
  948. module_put(nvmem->owner);
  949. kref_put(&nvmem->refcnt, nvmem_device_release);
  950. }
  951. #if IS_ENABLED(CONFIG_OF)
  952. /**
  953. * of_nvmem_device_get() - Get nvmem device from a given id
  954. *
  955. * @np: Device tree node that uses the nvmem device.
  956. * @id: nvmem name from nvmem-names property.
  957. *
  958. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  959. * on success.
  960. */
  961. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  962. {
  963. struct device_node *nvmem_np;
  964. struct nvmem_device *nvmem;
  965. int index = 0;
  966. if (id)
  967. index = of_property_match_string(np, "nvmem-names", id);
  968. nvmem_np = of_parse_phandle(np, "nvmem", index);
  969. if (!nvmem_np)
  970. return ERR_PTR(-ENOENT);
  971. nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
  972. of_node_put(nvmem_np);
  973. return nvmem;
  974. }
  975. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  976. #endif
  977. /**
  978. * nvmem_device_get() - Get nvmem device from a given id
  979. *
  980. * @dev: Device that uses the nvmem device.
  981. * @dev_name: name of the requested nvmem device.
  982. *
  983. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  984. * on success.
  985. */
  986. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  987. {
  988. if (dev->of_node) { /* try dt first */
  989. struct nvmem_device *nvmem;
  990. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  991. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  992. return nvmem;
  993. }
  994. return __nvmem_device_get((void *)dev_name, device_match_name);
  995. }
  996. EXPORT_SYMBOL_GPL(nvmem_device_get);
  997. /**
  998. * nvmem_device_find() - Find nvmem device with matching function
  999. *
  1000. * @data: Data to pass to match function
  1001. * @match: Callback function to check device
  1002. *
  1003. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  1004. * on success.
  1005. */
  1006. struct nvmem_device *nvmem_device_find(void *data,
  1007. int (*match)(struct device *dev, const void *data))
  1008. {
  1009. return __nvmem_device_get(data, match);
  1010. }
  1011. EXPORT_SYMBOL_GPL(nvmem_device_find);
  1012. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  1013. {
  1014. struct nvmem_device **nvmem = res;
  1015. if (WARN_ON(!nvmem || !*nvmem))
  1016. return 0;
  1017. return *nvmem == data;
  1018. }
  1019. static void devm_nvmem_device_release(struct device *dev, void *res)
  1020. {
  1021. nvmem_device_put(*(struct nvmem_device **)res);
  1022. }
  1023. /**
  1024. * devm_nvmem_device_put() - put alredy got nvmem device
  1025. *
  1026. * @dev: Device that uses the nvmem device.
  1027. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  1028. * that needs to be released.
  1029. */
  1030. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  1031. {
  1032. int ret;
  1033. ret = devres_release(dev, devm_nvmem_device_release,
  1034. devm_nvmem_device_match, nvmem);
  1035. WARN_ON(ret);
  1036. }
  1037. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  1038. /**
  1039. * nvmem_device_put() - put alredy got nvmem device
  1040. *
  1041. * @nvmem: pointer to nvmem device that needs to be released.
  1042. */
  1043. void nvmem_device_put(struct nvmem_device *nvmem)
  1044. {
  1045. __nvmem_device_put(nvmem);
  1046. }
  1047. EXPORT_SYMBOL_GPL(nvmem_device_put);
  1048. /**
  1049. * devm_nvmem_device_get() - Get nvmem device of device form a given id
  1050. *
  1051. * @dev: Device that requests the nvmem device.
  1052. * @id: name id for the requested nvmem device.
  1053. *
  1054. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  1055. * on success. The nvmem_device will be freed by the automatically once the
  1056. * device is freed.
  1057. */
  1058. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  1059. {
  1060. struct nvmem_device **ptr, *nvmem;
  1061. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  1062. if (!ptr)
  1063. return ERR_PTR(-ENOMEM);
  1064. nvmem = nvmem_device_get(dev, id);
  1065. if (!IS_ERR(nvmem)) {
  1066. *ptr = nvmem;
  1067. devres_add(dev, ptr);
  1068. } else {
  1069. devres_free(ptr);
  1070. }
  1071. return nvmem;
  1072. }
  1073. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  1074. static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
  1075. const char *id, int index)
  1076. {
  1077. struct nvmem_cell *cell;
  1078. const char *name = NULL;
  1079. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  1080. if (!cell)
  1081. return ERR_PTR(-ENOMEM);
  1082. if (id) {
  1083. name = kstrdup_const(id, GFP_KERNEL);
  1084. if (!name) {
  1085. kfree(cell);
  1086. return ERR_PTR(-ENOMEM);
  1087. }
  1088. }
  1089. cell->id = name;
  1090. cell->entry = entry;
  1091. cell->index = index;
  1092. return cell;
  1093. }
  1094. static struct nvmem_cell *
  1095. nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
  1096. {
  1097. struct nvmem_cell_entry *cell_entry;
  1098. struct nvmem_cell *cell = ERR_PTR(-ENOENT);
  1099. struct nvmem_cell_lookup *lookup;
  1100. struct nvmem_device *nvmem;
  1101. const char *dev_id;
  1102. if (!dev)
  1103. return ERR_PTR(-EINVAL);
  1104. dev_id = dev_name(dev);
  1105. mutex_lock(&nvmem_lookup_mutex);
  1106. list_for_each_entry(lookup, &nvmem_lookup_list, node) {
  1107. if ((strcmp(lookup->dev_id, dev_id) == 0) &&
  1108. (strcmp(lookup->con_id, con_id) == 0)) {
  1109. /* This is the right entry. */
  1110. nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
  1111. device_match_name);
  1112. if (IS_ERR(nvmem)) {
  1113. /* Provider may not be registered yet. */
  1114. cell = ERR_CAST(nvmem);
  1115. break;
  1116. }
  1117. cell_entry = nvmem_find_cell_entry_by_name(nvmem,
  1118. lookup->cell_name);
  1119. if (!cell_entry) {
  1120. __nvmem_device_put(nvmem);
  1121. cell = ERR_PTR(-ENOENT);
  1122. } else {
  1123. cell = nvmem_create_cell(cell_entry, con_id, 0);
  1124. if (IS_ERR(cell))
  1125. __nvmem_device_put(nvmem);
  1126. }
  1127. break;
  1128. }
  1129. }
  1130. mutex_unlock(&nvmem_lookup_mutex);
  1131. return cell;
  1132. }
  1133. static void nvmem_layout_module_put(struct nvmem_device *nvmem)
  1134. {
  1135. if (nvmem->layout && nvmem->layout->dev.driver)
  1136. module_put(nvmem->layout->dev.driver->owner);
  1137. }
  1138. #if IS_ENABLED(CONFIG_OF)
  1139. static struct nvmem_cell_entry *
  1140. nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
  1141. {
  1142. struct nvmem_cell_entry *iter, *cell = NULL;
  1143. mutex_lock(&nvmem_mutex);
  1144. list_for_each_entry(iter, &nvmem->cells, node) {
  1145. if (np == iter->np) {
  1146. cell = iter;
  1147. break;
  1148. }
  1149. }
  1150. mutex_unlock(&nvmem_mutex);
  1151. return cell;
  1152. }
  1153. static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
  1154. {
  1155. if (!nvmem->layout)
  1156. return 0;
  1157. if (!nvmem->layout->dev.driver ||
  1158. !try_module_get(nvmem->layout->dev.driver->owner))
  1159. return -EPROBE_DEFER;
  1160. return 0;
  1161. }
  1162. /**
  1163. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  1164. *
  1165. * @np: Device tree node that uses the nvmem cell.
  1166. * @id: nvmem cell name from nvmem-cell-names property, or NULL
  1167. * for the cell at index 0 (the lone cell with no accompanying
  1168. * nvmem-cell-names property).
  1169. *
  1170. * Return: Will be an ERR_PTR() on error or a valid pointer
  1171. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  1172. * nvmem_cell_put().
  1173. */
  1174. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
  1175. {
  1176. struct device_node *cell_np, *nvmem_np;
  1177. struct nvmem_device *nvmem;
  1178. struct nvmem_cell_entry *cell_entry;
  1179. struct nvmem_cell *cell;
  1180. struct of_phandle_args cell_spec;
  1181. int index = 0;
  1182. int cell_index = 0;
  1183. int ret;
  1184. /* if cell name exists, find index to the name */
  1185. if (id)
  1186. index = of_property_match_string(np, "nvmem-cell-names", id);
  1187. ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
  1188. "#nvmem-cell-cells",
  1189. index, &cell_spec);
  1190. if (ret)
  1191. return ERR_PTR(-ENOENT);
  1192. if (cell_spec.args_count > 1)
  1193. return ERR_PTR(-EINVAL);
  1194. cell_np = cell_spec.np;
  1195. if (cell_spec.args_count)
  1196. cell_index = cell_spec.args[0];
  1197. nvmem_np = of_get_parent(cell_np);
  1198. if (!nvmem_np) {
  1199. of_node_put(cell_np);
  1200. return ERR_PTR(-EINVAL);
  1201. }
  1202. /* nvmem layouts produce cells within the nvmem-layout container */
  1203. if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
  1204. nvmem_np = of_get_next_parent(nvmem_np);
  1205. if (!nvmem_np) {
  1206. of_node_put(cell_np);
  1207. return ERR_PTR(-EINVAL);
  1208. }
  1209. }
  1210. nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
  1211. of_node_put(nvmem_np);
  1212. if (IS_ERR(nvmem)) {
  1213. of_node_put(cell_np);
  1214. return ERR_CAST(nvmem);
  1215. }
  1216. ret = nvmem_layout_module_get_optional(nvmem);
  1217. if (ret) {
  1218. of_node_put(cell_np);
  1219. __nvmem_device_put(nvmem);
  1220. return ERR_PTR(ret);
  1221. }
  1222. cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
  1223. of_node_put(cell_np);
  1224. if (!cell_entry) {
  1225. __nvmem_device_put(nvmem);
  1226. nvmem_layout_module_put(nvmem);
  1227. if (nvmem->layout)
  1228. return ERR_PTR(-EPROBE_DEFER);
  1229. else
  1230. return ERR_PTR(-ENOENT);
  1231. }
  1232. cell = nvmem_create_cell(cell_entry, id, cell_index);
  1233. if (IS_ERR(cell)) {
  1234. __nvmem_device_put(nvmem);
  1235. nvmem_layout_module_put(nvmem);
  1236. }
  1237. return cell;
  1238. }
  1239. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  1240. #endif
  1241. /**
  1242. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  1243. *
  1244. * @dev: Device that requests the nvmem cell.
  1245. * @id: nvmem cell name to get (this corresponds with the name from the
  1246. * nvmem-cell-names property for DT systems and with the con_id from
  1247. * the lookup entry for non-DT systems).
  1248. *
  1249. * Return: Will be an ERR_PTR() on error or a valid pointer
  1250. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  1251. * nvmem_cell_put().
  1252. */
  1253. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
  1254. {
  1255. struct nvmem_cell *cell;
  1256. if (dev->of_node) { /* try dt first */
  1257. cell = of_nvmem_cell_get(dev->of_node, id);
  1258. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  1259. return cell;
  1260. }
  1261. /* NULL cell id only allowed for device tree; invalid otherwise */
  1262. if (!id)
  1263. return ERR_PTR(-EINVAL);
  1264. return nvmem_cell_get_from_lookup(dev, id);
  1265. }
  1266. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  1267. static void devm_nvmem_cell_release(struct device *dev, void *res)
  1268. {
  1269. nvmem_cell_put(*(struct nvmem_cell **)res);
  1270. }
  1271. /**
  1272. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  1273. *
  1274. * @dev: Device that requests the nvmem cell.
  1275. * @id: nvmem cell name id to get.
  1276. *
  1277. * Return: Will be an ERR_PTR() on error or a valid pointer
  1278. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  1279. * automatically once the device is freed.
  1280. */
  1281. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  1282. {
  1283. struct nvmem_cell **ptr, *cell;
  1284. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  1285. if (!ptr)
  1286. return ERR_PTR(-ENOMEM);
  1287. cell = nvmem_cell_get(dev, id);
  1288. if (!IS_ERR(cell)) {
  1289. *ptr = cell;
  1290. devres_add(dev, ptr);
  1291. } else {
  1292. devres_free(ptr);
  1293. }
  1294. return cell;
  1295. }
  1296. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  1297. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  1298. {
  1299. struct nvmem_cell **c = res;
  1300. if (WARN_ON(!c || !*c))
  1301. return 0;
  1302. return *c == data;
  1303. }
  1304. /**
  1305. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  1306. * from devm_nvmem_cell_get.
  1307. *
  1308. * @dev: Device that requests the nvmem cell.
  1309. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
  1310. */
  1311. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  1312. {
  1313. int ret;
  1314. ret = devres_release(dev, devm_nvmem_cell_release,
  1315. devm_nvmem_cell_match, cell);
  1316. WARN_ON(ret);
  1317. }
  1318. EXPORT_SYMBOL(devm_nvmem_cell_put);
  1319. /**
  1320. * nvmem_cell_put() - Release previously allocated nvmem cell.
  1321. *
  1322. * @cell: Previously allocated nvmem cell by nvmem_cell_get().
  1323. */
  1324. void nvmem_cell_put(struct nvmem_cell *cell)
  1325. {
  1326. struct nvmem_device *nvmem = cell->entry->nvmem;
  1327. if (cell->id)
  1328. kfree_const(cell->id);
  1329. kfree(cell);
  1330. __nvmem_device_put(nvmem);
  1331. nvmem_layout_module_put(nvmem);
  1332. }
  1333. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  1334. static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
  1335. {
  1336. u8 *p, *b;
  1337. int i, extra, bytes_offset;
  1338. int bit_offset = cell->bit_offset;
  1339. p = b = buf;
  1340. bytes_offset = bit_offset / BITS_PER_BYTE;
  1341. b += bytes_offset;
  1342. bit_offset %= BITS_PER_BYTE;
  1343. if (bit_offset % BITS_PER_BYTE) {
  1344. /* First shift */
  1345. *p = *b++ >> bit_offset;
  1346. /* setup rest of the bytes if any */
  1347. for (i = 1; i < cell->bytes; i++) {
  1348. /* Get bits from next byte and shift them towards msb */
  1349. *p++ |= *b << (BITS_PER_BYTE - bit_offset);
  1350. *p = *b++ >> bit_offset;
  1351. }
  1352. } else if (p != b) {
  1353. memmove(p, b, cell->bytes - bytes_offset);
  1354. p += cell->bytes - 1;
  1355. } else {
  1356. /* point to the msb */
  1357. p += cell->bytes - 1;
  1358. }
  1359. /* result fits in less bytes */
  1360. extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
  1361. while (--extra >= 0)
  1362. *p-- = 0;
  1363. /* clear msb bits if any leftover in the last byte */
  1364. if (cell->nbits % BITS_PER_BYTE)
  1365. *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
  1366. }
  1367. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  1368. struct nvmem_cell_entry *cell,
  1369. void *buf, size_t *len, const char *id, int index)
  1370. {
  1371. int rc;
  1372. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
  1373. if (rc)
  1374. return rc;
  1375. /* shift bits in-place */
  1376. if (cell->bit_offset || cell->nbits)
  1377. nvmem_shift_read_buffer_in_place(cell, buf);
  1378. if (cell->read_post_process) {
  1379. rc = cell->read_post_process(cell->priv, id, index,
  1380. cell->offset, buf, cell->raw_len);
  1381. if (rc)
  1382. return rc;
  1383. }
  1384. if (len)
  1385. *len = cell->bytes;
  1386. return 0;
  1387. }
  1388. /**
  1389. * nvmem_cell_read() - Read a given nvmem cell
  1390. *
  1391. * @cell: nvmem cell to be read.
  1392. * @len: pointer to length of cell which will be populated on successful read;
  1393. * can be NULL.
  1394. *
  1395. * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
  1396. * buffer should be freed by the consumer with a kfree().
  1397. */
  1398. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  1399. {
  1400. struct nvmem_cell_entry *entry = cell->entry;
  1401. struct nvmem_device *nvmem = entry->nvmem;
  1402. u8 *buf;
  1403. int rc;
  1404. if (!nvmem)
  1405. return ERR_PTR(-EINVAL);
  1406. buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL);
  1407. if (!buf)
  1408. return ERR_PTR(-ENOMEM);
  1409. rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
  1410. if (rc) {
  1411. kfree(buf);
  1412. return ERR_PTR(rc);
  1413. }
  1414. return buf;
  1415. }
  1416. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  1417. static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
  1418. u8 *_buf, int len)
  1419. {
  1420. struct nvmem_device *nvmem = cell->nvmem;
  1421. int i, rc, nbits, bit_offset = cell->bit_offset;
  1422. u8 v, *p, *buf, *b, pbyte, pbits;
  1423. nbits = cell->nbits;
  1424. buf = kzalloc(cell->bytes, GFP_KERNEL);
  1425. if (!buf)
  1426. return ERR_PTR(-ENOMEM);
  1427. memcpy(buf, _buf, len);
  1428. p = b = buf;
  1429. if (bit_offset) {
  1430. pbyte = *b;
  1431. *b <<= bit_offset;
  1432. /* setup the first byte with lsb bits from nvmem */
  1433. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  1434. if (rc)
  1435. goto err;
  1436. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  1437. /* setup rest of the byte if any */
  1438. for (i = 1; i < cell->bytes; i++) {
  1439. /* Get last byte bits and shift them towards lsb */
  1440. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  1441. pbyte = *b;
  1442. p = b;
  1443. *b <<= bit_offset;
  1444. *b++ |= pbits;
  1445. }
  1446. }
  1447. /* if it's not end on byte boundary */
  1448. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  1449. /* setup the last byte with msb bits from nvmem */
  1450. rc = nvmem_reg_read(nvmem,
  1451. cell->offset + cell->bytes - 1, &v, 1);
  1452. if (rc)
  1453. goto err;
  1454. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  1455. }
  1456. return buf;
  1457. err:
  1458. kfree(buf);
  1459. return ERR_PTR(rc);
  1460. }
  1461. static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
  1462. {
  1463. struct nvmem_device *nvmem = cell->nvmem;
  1464. int rc;
  1465. if (!nvmem || nvmem->read_only ||
  1466. (cell->bit_offset == 0 && len != cell->bytes))
  1467. return -EINVAL;
  1468. /*
  1469. * Any cells which have a read_post_process hook are read-only because
  1470. * we cannot reverse the operation and it might affect other cells,
  1471. * too.
  1472. */
  1473. if (cell->read_post_process)
  1474. return -EINVAL;
  1475. if (cell->bit_offset || cell->nbits) {
  1476. if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes)
  1477. return -EINVAL;
  1478. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  1479. if (IS_ERR(buf))
  1480. return PTR_ERR(buf);
  1481. }
  1482. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  1483. /* free the tmp buffer */
  1484. if (cell->bit_offset || cell->nbits)
  1485. kfree(buf);
  1486. if (rc)
  1487. return rc;
  1488. return len;
  1489. }
  1490. /**
  1491. * nvmem_cell_write() - Write to a given nvmem cell
  1492. *
  1493. * @cell: nvmem cell to be written.
  1494. * @buf: Buffer to be written.
  1495. * @len: length of buffer to be written to nvmem cell.
  1496. *
  1497. * Return: length of bytes written or negative on failure.
  1498. */
  1499. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  1500. {
  1501. return __nvmem_cell_entry_write(cell->entry, buf, len);
  1502. }
  1503. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  1504. static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
  1505. void *val, size_t count)
  1506. {
  1507. struct nvmem_cell *cell;
  1508. void *buf;
  1509. size_t len;
  1510. cell = nvmem_cell_get(dev, cell_id);
  1511. if (IS_ERR(cell))
  1512. return PTR_ERR(cell);
  1513. buf = nvmem_cell_read(cell, &len);
  1514. if (IS_ERR(buf)) {
  1515. nvmem_cell_put(cell);
  1516. return PTR_ERR(buf);
  1517. }
  1518. if (len != count) {
  1519. kfree(buf);
  1520. nvmem_cell_put(cell);
  1521. return -EINVAL;
  1522. }
  1523. memcpy(val, buf, count);
  1524. kfree(buf);
  1525. nvmem_cell_put(cell);
  1526. return 0;
  1527. }
  1528. /**
  1529. * nvmem_cell_read_u8() - Read a cell value as a u8
  1530. *
  1531. * @dev: Device that requests the nvmem cell.
  1532. * @cell_id: Name of nvmem cell to read.
  1533. * @val: pointer to output value.
  1534. *
  1535. * Return: 0 on success or negative errno.
  1536. */
  1537. int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
  1538. {
  1539. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1540. }
  1541. EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
  1542. /**
  1543. * nvmem_cell_read_u16() - Read a cell value as a u16
  1544. *
  1545. * @dev: Device that requests the nvmem cell.
  1546. * @cell_id: Name of nvmem cell to read.
  1547. * @val: pointer to output value.
  1548. *
  1549. * Return: 0 on success or negative errno.
  1550. */
  1551. int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
  1552. {
  1553. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1554. }
  1555. EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
  1556. /**
  1557. * nvmem_cell_read_u32() - Read a cell value as a u32
  1558. *
  1559. * @dev: Device that requests the nvmem cell.
  1560. * @cell_id: Name of nvmem cell to read.
  1561. * @val: pointer to output value.
  1562. *
  1563. * Return: 0 on success or negative errno.
  1564. */
  1565. int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
  1566. {
  1567. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1568. }
  1569. EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
  1570. /**
  1571. * nvmem_cell_read_u64() - Read a cell value as a u64
  1572. *
  1573. * @dev: Device that requests the nvmem cell.
  1574. * @cell_id: Name of nvmem cell to read.
  1575. * @val: pointer to output value.
  1576. *
  1577. * Return: 0 on success or negative errno.
  1578. */
  1579. int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
  1580. {
  1581. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1582. }
  1583. EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
  1584. static const void *nvmem_cell_read_variable_common(struct device *dev,
  1585. const char *cell_id,
  1586. size_t max_len, size_t *len)
  1587. {
  1588. struct nvmem_cell *cell;
  1589. int nbits;
  1590. void *buf;
  1591. cell = nvmem_cell_get(dev, cell_id);
  1592. if (IS_ERR(cell))
  1593. return cell;
  1594. nbits = cell->entry->nbits;
  1595. buf = nvmem_cell_read(cell, len);
  1596. nvmem_cell_put(cell);
  1597. if (IS_ERR(buf))
  1598. return buf;
  1599. /*
  1600. * If nbits is set then nvmem_cell_read() can significantly exaggerate
  1601. * the length of the real data. Throw away the extra junk.
  1602. */
  1603. if (nbits)
  1604. *len = DIV_ROUND_UP(nbits, 8);
  1605. if (*len > max_len) {
  1606. kfree(buf);
  1607. return ERR_PTR(-ERANGE);
  1608. }
  1609. return buf;
  1610. }
  1611. /**
  1612. * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
  1613. *
  1614. * @dev: Device that requests the nvmem cell.
  1615. * @cell_id: Name of nvmem cell to read.
  1616. * @val: pointer to output value.
  1617. *
  1618. * Return: 0 on success or negative errno.
  1619. */
  1620. int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
  1621. u32 *val)
  1622. {
  1623. size_t len;
  1624. const u8 *buf;
  1625. int i;
  1626. buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
  1627. if (IS_ERR(buf))
  1628. return PTR_ERR(buf);
  1629. /* Copy w/ implicit endian conversion */
  1630. *val = 0;
  1631. for (i = 0; i < len; i++)
  1632. *val |= buf[i] << (8 * i);
  1633. kfree(buf);
  1634. return 0;
  1635. }
  1636. EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
  1637. /**
  1638. * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
  1639. *
  1640. * @dev: Device that requests the nvmem cell.
  1641. * @cell_id: Name of nvmem cell to read.
  1642. * @val: pointer to output value.
  1643. *
  1644. * Return: 0 on success or negative errno.
  1645. */
  1646. int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
  1647. u64 *val)
  1648. {
  1649. size_t len;
  1650. const u8 *buf;
  1651. int i;
  1652. buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
  1653. if (IS_ERR(buf))
  1654. return PTR_ERR(buf);
  1655. /* Copy w/ implicit endian conversion */
  1656. *val = 0;
  1657. for (i = 0; i < len; i++)
  1658. *val |= (uint64_t)buf[i] << (8 * i);
  1659. kfree(buf);
  1660. return 0;
  1661. }
  1662. EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
  1663. /**
  1664. * nvmem_device_cell_read() - Read a given nvmem device and cell
  1665. *
  1666. * @nvmem: nvmem device to read from.
  1667. * @info: nvmem cell info to be read.
  1668. * @buf: buffer pointer which will be populated on successful read.
  1669. *
  1670. * Return: length of successful bytes read on success and negative
  1671. * error code on error.
  1672. */
  1673. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  1674. struct nvmem_cell_info *info, void *buf)
  1675. {
  1676. struct nvmem_cell_entry cell;
  1677. int rc;
  1678. ssize_t len;
  1679. if (!nvmem)
  1680. return -EINVAL;
  1681. rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
  1682. if (rc)
  1683. return rc;
  1684. rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
  1685. if (rc)
  1686. return rc;
  1687. return len;
  1688. }
  1689. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  1690. /**
  1691. * nvmem_device_cell_write() - Write cell to a given nvmem device
  1692. *
  1693. * @nvmem: nvmem device to be written to.
  1694. * @info: nvmem cell info to be written.
  1695. * @buf: buffer to be written to cell.
  1696. *
  1697. * Return: length of bytes written or negative error code on failure.
  1698. */
  1699. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  1700. struct nvmem_cell_info *info, void *buf)
  1701. {
  1702. struct nvmem_cell_entry cell;
  1703. int rc;
  1704. if (!nvmem)
  1705. return -EINVAL;
  1706. rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
  1707. if (rc)
  1708. return rc;
  1709. return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
  1710. }
  1711. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  1712. /**
  1713. * nvmem_device_read() - Read from a given nvmem device
  1714. *
  1715. * @nvmem: nvmem device to read from.
  1716. * @offset: offset in nvmem device.
  1717. * @bytes: number of bytes to read.
  1718. * @buf: buffer pointer which will be populated on successful read.
  1719. *
  1720. * Return: length of successful bytes read on success and negative
  1721. * error code on error.
  1722. */
  1723. int nvmem_device_read(struct nvmem_device *nvmem,
  1724. unsigned int offset,
  1725. size_t bytes, void *buf)
  1726. {
  1727. int rc;
  1728. if (!nvmem)
  1729. return -EINVAL;
  1730. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  1731. if (rc)
  1732. return rc;
  1733. return bytes;
  1734. }
  1735. EXPORT_SYMBOL_GPL(nvmem_device_read);
  1736. /**
  1737. * nvmem_device_write() - Write cell to a given nvmem device
  1738. *
  1739. * @nvmem: nvmem device to be written to.
  1740. * @offset: offset in nvmem device.
  1741. * @bytes: number of bytes to write.
  1742. * @buf: buffer to be written.
  1743. *
  1744. * Return: length of bytes written or negative error code on failure.
  1745. */
  1746. int nvmem_device_write(struct nvmem_device *nvmem,
  1747. unsigned int offset,
  1748. size_t bytes, void *buf)
  1749. {
  1750. int rc;
  1751. if (!nvmem)
  1752. return -EINVAL;
  1753. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  1754. if (rc)
  1755. return rc;
  1756. return bytes;
  1757. }
  1758. EXPORT_SYMBOL_GPL(nvmem_device_write);
  1759. /**
  1760. * nvmem_add_cell_table() - register a table of cell info entries
  1761. *
  1762. * @table: table of cell info entries
  1763. */
  1764. void nvmem_add_cell_table(struct nvmem_cell_table *table)
  1765. {
  1766. mutex_lock(&nvmem_cell_mutex);
  1767. list_add_tail(&table->node, &nvmem_cell_tables);
  1768. mutex_unlock(&nvmem_cell_mutex);
  1769. }
  1770. EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
  1771. /**
  1772. * nvmem_del_cell_table() - remove a previously registered cell info table
  1773. *
  1774. * @table: table of cell info entries
  1775. */
  1776. void nvmem_del_cell_table(struct nvmem_cell_table *table)
  1777. {
  1778. mutex_lock(&nvmem_cell_mutex);
  1779. list_del(&table->node);
  1780. mutex_unlock(&nvmem_cell_mutex);
  1781. }
  1782. EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
  1783. /**
  1784. * nvmem_add_cell_lookups() - register a list of cell lookup entries
  1785. *
  1786. * @entries: array of cell lookup entries
  1787. * @nentries: number of cell lookup entries in the array
  1788. */
  1789. void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
  1790. {
  1791. int i;
  1792. mutex_lock(&nvmem_lookup_mutex);
  1793. for (i = 0; i < nentries; i++)
  1794. list_add_tail(&entries[i].node, &nvmem_lookup_list);
  1795. mutex_unlock(&nvmem_lookup_mutex);
  1796. }
  1797. EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
  1798. /**
  1799. * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
  1800. * entries
  1801. *
  1802. * @entries: array of cell lookup entries
  1803. * @nentries: number of cell lookup entries in the array
  1804. */
  1805. void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
  1806. {
  1807. int i;
  1808. mutex_lock(&nvmem_lookup_mutex);
  1809. for (i = 0; i < nentries; i++)
  1810. list_del(&entries[i].node);
  1811. mutex_unlock(&nvmem_lookup_mutex);
  1812. }
  1813. EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
  1814. /**
  1815. * nvmem_dev_name() - Get the name of a given nvmem device.
  1816. *
  1817. * @nvmem: nvmem device.
  1818. *
  1819. * Return: name of the nvmem device.
  1820. */
  1821. const char *nvmem_dev_name(struct nvmem_device *nvmem)
  1822. {
  1823. return dev_name(&nvmem->dev);
  1824. }
  1825. EXPORT_SYMBOL_GPL(nvmem_dev_name);
  1826. /**
  1827. * nvmem_dev_size() - Get the size of a given nvmem device.
  1828. *
  1829. * @nvmem: nvmem device.
  1830. *
  1831. * Return: size of the nvmem device.
  1832. */
  1833. size_t nvmem_dev_size(struct nvmem_device *nvmem)
  1834. {
  1835. return nvmem->size;
  1836. }
  1837. EXPORT_SYMBOL_GPL(nvmem_dev_size);
  1838. static int __init nvmem_init(void)
  1839. {
  1840. int ret;
  1841. ret = bus_register(&nvmem_bus_type);
  1842. if (ret)
  1843. return ret;
  1844. ret = nvmem_layout_bus_register();
  1845. if (ret)
  1846. bus_unregister(&nvmem_bus_type);
  1847. return ret;
  1848. }
  1849. static void __exit nvmem_exit(void)
  1850. {
  1851. nvmem_layout_bus_unregister();
  1852. bus_unregister(&nvmem_bus_type);
  1853. }
  1854. subsys_initcall(nvmem_init);
  1855. module_exit(nvmem_exit);
  1856. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  1857. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  1858. MODULE_DESCRIPTION("nvmem Driver Core");