core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*
  2. * nvmem framework core.
  3. *
  4. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  5. * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/export.h>
  18. #include <linux/fs.h>
  19. #include <linux/idr.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/nvmem-consumer.h>
  23. #include <linux/nvmem-provider.h>
  24. #include <linux/of.h>
  25. #include <linux/slab.h>
  26. struct nvmem_device {
  27. const char *name;
  28. struct module *owner;
  29. struct device dev;
  30. int stride;
  31. int word_size;
  32. int ncells;
  33. int id;
  34. int users;
  35. size_t size;
  36. bool read_only;
  37. int flags;
  38. struct bin_attribute eeprom;
  39. struct device *base_dev;
  40. nvmem_reg_read_t reg_read;
  41. nvmem_reg_write_t reg_write;
  42. void *priv;
  43. };
  44. #define FLAG_COMPAT BIT(0)
  45. struct nvmem_cell {
  46. const char *name;
  47. int offset;
  48. int bytes;
  49. int bit_offset;
  50. int nbits;
  51. struct nvmem_device *nvmem;
  52. struct list_head node;
  53. };
  54. static DEFINE_MUTEX(nvmem_mutex);
  55. static DEFINE_IDA(nvmem_ida);
  56. static LIST_HEAD(nvmem_cells);
  57. static DEFINE_MUTEX(nvmem_cells_mutex);
  58. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  59. static struct lock_class_key eeprom_lock_key;
  60. #endif
  61. #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  62. static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  63. void *val, size_t bytes)
  64. {
  65. if (nvmem->reg_read)
  66. return nvmem->reg_read(nvmem->priv, offset, val, bytes);
  67. return -EINVAL;
  68. }
  69. static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  70. void *val, size_t bytes)
  71. {
  72. if (nvmem->reg_write)
  73. return nvmem->reg_write(nvmem->priv, offset, val, bytes);
  74. return -EINVAL;
  75. }
  76. static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
  77. struct bin_attribute *attr,
  78. char *buf, loff_t pos, size_t count)
  79. {
  80. struct device *dev;
  81. struct nvmem_device *nvmem;
  82. int rc;
  83. if (attr->private)
  84. dev = attr->private;
  85. else
  86. dev = container_of(kobj, struct device, kobj);
  87. nvmem = to_nvmem_device(dev);
  88. /* Stop the user from reading */
  89. if (pos >= nvmem->size)
  90. return 0;
  91. if (count < nvmem->word_size)
  92. return -EINVAL;
  93. if (pos + count > nvmem->size)
  94. count = nvmem->size - pos;
  95. count = round_down(count, nvmem->word_size);
  96. rc = nvmem_reg_read(nvmem, pos, buf, count);
  97. if (rc)
  98. return rc;
  99. return count;
  100. }
  101. static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
  102. struct bin_attribute *attr,
  103. char *buf, loff_t pos, size_t count)
  104. {
  105. struct device *dev;
  106. struct nvmem_device *nvmem;
  107. int rc;
  108. if (attr->private)
  109. dev = attr->private;
  110. else
  111. dev = container_of(kobj, struct device, kobj);
  112. nvmem = to_nvmem_device(dev);
  113. /* Stop the user from writing */
  114. if (pos >= nvmem->size)
  115. return 0;
  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. rc = nvmem_reg_write(nvmem, pos, buf, count);
  122. if (rc)
  123. return rc;
  124. return count;
  125. }
  126. /* default read/write permissions */
  127. static struct bin_attribute bin_attr_rw_nvmem = {
  128. .attr = {
  129. .name = "nvmem",
  130. .mode = S_IWUSR | S_IRUGO,
  131. },
  132. .read = bin_attr_nvmem_read,
  133. .write = bin_attr_nvmem_write,
  134. };
  135. static struct bin_attribute *nvmem_bin_rw_attributes[] = {
  136. &bin_attr_rw_nvmem,
  137. NULL,
  138. };
  139. static const struct attribute_group nvmem_bin_rw_group = {
  140. .bin_attrs = nvmem_bin_rw_attributes,
  141. };
  142. static const struct attribute_group *nvmem_rw_dev_groups[] = {
  143. &nvmem_bin_rw_group,
  144. NULL,
  145. };
  146. /* read only permission */
  147. static struct bin_attribute bin_attr_ro_nvmem = {
  148. .attr = {
  149. .name = "nvmem",
  150. .mode = S_IRUGO,
  151. },
  152. .read = bin_attr_nvmem_read,
  153. };
  154. static struct bin_attribute *nvmem_bin_ro_attributes[] = {
  155. &bin_attr_ro_nvmem,
  156. NULL,
  157. };
  158. static const struct attribute_group nvmem_bin_ro_group = {
  159. .bin_attrs = nvmem_bin_ro_attributes,
  160. };
  161. static const struct attribute_group *nvmem_ro_dev_groups[] = {
  162. &nvmem_bin_ro_group,
  163. NULL,
  164. };
  165. /* default read/write permissions, root only */
  166. static struct bin_attribute bin_attr_rw_root_nvmem = {
  167. .attr = {
  168. .name = "nvmem",
  169. .mode = S_IWUSR | S_IRUSR,
  170. },
  171. .read = bin_attr_nvmem_read,
  172. .write = bin_attr_nvmem_write,
  173. };
  174. static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
  175. &bin_attr_rw_root_nvmem,
  176. NULL,
  177. };
  178. static const struct attribute_group nvmem_bin_rw_root_group = {
  179. .bin_attrs = nvmem_bin_rw_root_attributes,
  180. };
  181. static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
  182. &nvmem_bin_rw_root_group,
  183. NULL,
  184. };
  185. /* read only permission, root only */
  186. static struct bin_attribute bin_attr_ro_root_nvmem = {
  187. .attr = {
  188. .name = "nvmem",
  189. .mode = S_IRUSR,
  190. },
  191. .read = bin_attr_nvmem_read,
  192. };
  193. static struct bin_attribute *nvmem_bin_ro_root_attributes[] = {
  194. &bin_attr_ro_root_nvmem,
  195. NULL,
  196. };
  197. static const struct attribute_group nvmem_bin_ro_root_group = {
  198. .bin_attrs = nvmem_bin_ro_root_attributes,
  199. };
  200. static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
  201. &nvmem_bin_ro_root_group,
  202. NULL,
  203. };
  204. static void nvmem_release(struct device *dev)
  205. {
  206. struct nvmem_device *nvmem = to_nvmem_device(dev);
  207. ida_simple_remove(&nvmem_ida, nvmem->id);
  208. kfree(nvmem);
  209. }
  210. static const struct device_type nvmem_provider_type = {
  211. .release = nvmem_release,
  212. };
  213. static struct bus_type nvmem_bus_type = {
  214. .name = "nvmem",
  215. };
  216. static int of_nvmem_match(struct device *dev, void *nvmem_np)
  217. {
  218. return dev->of_node == nvmem_np;
  219. }
  220. static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
  221. {
  222. struct device *d;
  223. if (!nvmem_np)
  224. return NULL;
  225. d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
  226. if (!d)
  227. return NULL;
  228. return to_nvmem_device(d);
  229. }
  230. static struct nvmem_cell *nvmem_find_cell(const char *cell_id)
  231. {
  232. struct nvmem_cell *p;
  233. list_for_each_entry(p, &nvmem_cells, node)
  234. if (p && !strcmp(p->name, cell_id))
  235. return p;
  236. return NULL;
  237. }
  238. static void nvmem_cell_drop(struct nvmem_cell *cell)
  239. {
  240. mutex_lock(&nvmem_cells_mutex);
  241. list_del(&cell->node);
  242. mutex_unlock(&nvmem_cells_mutex);
  243. kfree(cell);
  244. }
  245. static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
  246. {
  247. struct nvmem_cell *cell;
  248. struct list_head *p, *n;
  249. list_for_each_safe(p, n, &nvmem_cells) {
  250. cell = list_entry(p, struct nvmem_cell, node);
  251. if (cell->nvmem == nvmem)
  252. nvmem_cell_drop(cell);
  253. }
  254. }
  255. static void nvmem_cell_add(struct nvmem_cell *cell)
  256. {
  257. mutex_lock(&nvmem_cells_mutex);
  258. list_add_tail(&cell->node, &nvmem_cells);
  259. mutex_unlock(&nvmem_cells_mutex);
  260. }
  261. static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
  262. const struct nvmem_cell_info *info,
  263. struct nvmem_cell *cell)
  264. {
  265. cell->nvmem = nvmem;
  266. cell->offset = info->offset;
  267. cell->bytes = info->bytes;
  268. cell->name = info->name;
  269. cell->bit_offset = info->bit_offset;
  270. cell->nbits = info->nbits;
  271. if (cell->nbits)
  272. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  273. BITS_PER_BYTE);
  274. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  275. dev_err(&nvmem->dev,
  276. "cell %s unaligned to nvmem stride %d\n",
  277. cell->name, nvmem->stride);
  278. return -EINVAL;
  279. }
  280. return 0;
  281. }
  282. static int nvmem_add_cells(struct nvmem_device *nvmem,
  283. const struct nvmem_config *cfg)
  284. {
  285. struct nvmem_cell **cells;
  286. const struct nvmem_cell_info *info = cfg->cells;
  287. int i, rval;
  288. cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL);
  289. if (!cells)
  290. return -ENOMEM;
  291. for (i = 0; i < cfg->ncells; i++) {
  292. cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
  293. if (!cells[i]) {
  294. rval = -ENOMEM;
  295. goto err;
  296. }
  297. rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
  298. if (rval) {
  299. kfree(cells[i]);
  300. goto err;
  301. }
  302. nvmem_cell_add(cells[i]);
  303. }
  304. nvmem->ncells = cfg->ncells;
  305. /* remove tmp array */
  306. kfree(cells);
  307. return 0;
  308. err:
  309. while (i--)
  310. nvmem_cell_drop(cells[i]);
  311. kfree(cells);
  312. return rval;
  313. }
  314. /*
  315. * nvmem_setup_compat() - Create an additional binary entry in
  316. * drivers sys directory, to be backwards compatible with the older
  317. * drivers/misc/eeprom drivers.
  318. */
  319. static int nvmem_setup_compat(struct nvmem_device *nvmem,
  320. const struct nvmem_config *config)
  321. {
  322. int rval;
  323. if (!config->base_dev)
  324. return -EINVAL;
  325. if (nvmem->read_only)
  326. nvmem->eeprom = bin_attr_ro_root_nvmem;
  327. else
  328. nvmem->eeprom = bin_attr_rw_root_nvmem;
  329. nvmem->eeprom.attr.name = "eeprom";
  330. nvmem->eeprom.size = nvmem->size;
  331. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  332. nvmem->eeprom.attr.key = &eeprom_lock_key;
  333. #endif
  334. nvmem->eeprom.private = &nvmem->dev;
  335. nvmem->base_dev = config->base_dev;
  336. rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
  337. if (rval) {
  338. dev_err(&nvmem->dev,
  339. "Failed to create eeprom binary file %d\n", rval);
  340. return rval;
  341. }
  342. nvmem->flags |= FLAG_COMPAT;
  343. return 0;
  344. }
  345. /**
  346. * nvmem_register() - Register a nvmem device for given nvmem_config.
  347. * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  348. *
  349. * @config: nvmem device configuration with which nvmem device is created.
  350. *
  351. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  352. * on success.
  353. */
  354. struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  355. {
  356. struct nvmem_device *nvmem;
  357. struct device_node *np;
  358. int rval;
  359. if (!config->dev)
  360. return ERR_PTR(-EINVAL);
  361. nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
  362. if (!nvmem)
  363. return ERR_PTR(-ENOMEM);
  364. rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
  365. if (rval < 0) {
  366. kfree(nvmem);
  367. return ERR_PTR(rval);
  368. }
  369. nvmem->id = rval;
  370. nvmem->owner = config->owner;
  371. nvmem->stride = config->stride;
  372. nvmem->word_size = config->word_size;
  373. nvmem->size = config->size;
  374. nvmem->dev.type = &nvmem_provider_type;
  375. nvmem->dev.bus = &nvmem_bus_type;
  376. nvmem->dev.parent = config->dev;
  377. nvmem->priv = config->priv;
  378. nvmem->reg_read = config->reg_read;
  379. nvmem->reg_write = config->reg_write;
  380. np = config->dev->of_node;
  381. nvmem->dev.of_node = np;
  382. dev_set_name(&nvmem->dev, "%s%d",
  383. config->name ? : "nvmem",
  384. config->name ? config->id : nvmem->id);
  385. nvmem->read_only = of_property_read_bool(np, "read-only") |
  386. config->read_only;
  387. if (config->root_only)
  388. nvmem->dev.groups = nvmem->read_only ?
  389. nvmem_ro_root_dev_groups :
  390. nvmem_rw_root_dev_groups;
  391. else
  392. nvmem->dev.groups = nvmem->read_only ?
  393. nvmem_ro_dev_groups :
  394. nvmem_rw_dev_groups;
  395. device_initialize(&nvmem->dev);
  396. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  397. rval = device_add(&nvmem->dev);
  398. if (rval)
  399. goto out;
  400. if (config->compat) {
  401. rval = nvmem_setup_compat(nvmem, config);
  402. if (rval)
  403. goto out;
  404. }
  405. if (config->cells)
  406. nvmem_add_cells(nvmem, config);
  407. return nvmem;
  408. out:
  409. ida_simple_remove(&nvmem_ida, nvmem->id);
  410. kfree(nvmem);
  411. return ERR_PTR(rval);
  412. }
  413. EXPORT_SYMBOL_GPL(nvmem_register);
  414. /**
  415. * nvmem_unregister() - Unregister previously registered nvmem device
  416. *
  417. * @nvmem: Pointer to previously registered nvmem device.
  418. *
  419. * Return: Will be an negative on error or a zero on success.
  420. */
  421. int nvmem_unregister(struct nvmem_device *nvmem)
  422. {
  423. mutex_lock(&nvmem_mutex);
  424. if (nvmem->users) {
  425. mutex_unlock(&nvmem_mutex);
  426. return -EBUSY;
  427. }
  428. mutex_unlock(&nvmem_mutex);
  429. if (nvmem->flags & FLAG_COMPAT)
  430. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  431. nvmem_device_remove_all_cells(nvmem);
  432. device_del(&nvmem->dev);
  433. return 0;
  434. }
  435. EXPORT_SYMBOL_GPL(nvmem_unregister);
  436. static struct nvmem_device *__nvmem_device_get(struct device_node *np,
  437. struct nvmem_cell **cellp,
  438. const char *cell_id)
  439. {
  440. struct nvmem_device *nvmem = NULL;
  441. mutex_lock(&nvmem_mutex);
  442. if (np) {
  443. nvmem = of_nvmem_find(np);
  444. if (!nvmem) {
  445. mutex_unlock(&nvmem_mutex);
  446. return ERR_PTR(-EPROBE_DEFER);
  447. }
  448. } else {
  449. struct nvmem_cell *cell = nvmem_find_cell(cell_id);
  450. if (cell) {
  451. nvmem = cell->nvmem;
  452. *cellp = cell;
  453. }
  454. if (!nvmem) {
  455. mutex_unlock(&nvmem_mutex);
  456. return ERR_PTR(-ENOENT);
  457. }
  458. }
  459. nvmem->users++;
  460. mutex_unlock(&nvmem_mutex);
  461. if (!try_module_get(nvmem->owner)) {
  462. dev_err(&nvmem->dev,
  463. "could not increase module refcount for cell %s\n",
  464. nvmem->name);
  465. mutex_lock(&nvmem_mutex);
  466. nvmem->users--;
  467. mutex_unlock(&nvmem_mutex);
  468. return ERR_PTR(-EINVAL);
  469. }
  470. return nvmem;
  471. }
  472. static void __nvmem_device_put(struct nvmem_device *nvmem)
  473. {
  474. module_put(nvmem->owner);
  475. mutex_lock(&nvmem_mutex);
  476. nvmem->users--;
  477. mutex_unlock(&nvmem_mutex);
  478. }
  479. static int nvmem_match(struct device *dev, void *data)
  480. {
  481. return !strcmp(dev_name(dev), data);
  482. }
  483. static struct nvmem_device *nvmem_find(const char *name)
  484. {
  485. struct device *d;
  486. d = bus_find_device(&nvmem_bus_type, NULL, (void *)name, nvmem_match);
  487. if (!d)
  488. return NULL;
  489. return to_nvmem_device(d);
  490. }
  491. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  492. /**
  493. * of_nvmem_device_get() - Get nvmem device from a given id
  494. *
  495. * @np: Device tree node that uses the nvmem device.
  496. * @id: nvmem name from nvmem-names property.
  497. *
  498. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  499. * on success.
  500. */
  501. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  502. {
  503. struct device_node *nvmem_np;
  504. int index;
  505. index = of_property_match_string(np, "nvmem-names", id);
  506. nvmem_np = of_parse_phandle(np, "nvmem", index);
  507. if (!nvmem_np)
  508. return ERR_PTR(-EINVAL);
  509. return __nvmem_device_get(nvmem_np, NULL, NULL);
  510. }
  511. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  512. #endif
  513. /**
  514. * nvmem_device_get() - Get nvmem device from a given id
  515. *
  516. * @dev: Device that uses the nvmem device.
  517. * @dev_name: name of the requested nvmem device.
  518. *
  519. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  520. * on success.
  521. */
  522. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  523. {
  524. if (dev->of_node) { /* try dt first */
  525. struct nvmem_device *nvmem;
  526. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  527. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  528. return nvmem;
  529. }
  530. return nvmem_find(dev_name);
  531. }
  532. EXPORT_SYMBOL_GPL(nvmem_device_get);
  533. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  534. {
  535. struct nvmem_device **nvmem = res;
  536. if (WARN_ON(!nvmem || !*nvmem))
  537. return 0;
  538. return *nvmem == data;
  539. }
  540. static void devm_nvmem_device_release(struct device *dev, void *res)
  541. {
  542. nvmem_device_put(*(struct nvmem_device **)res);
  543. }
  544. /**
  545. * devm_nvmem_device_put() - put alredy got nvmem device
  546. *
  547. * @dev: Device that uses the nvmem device.
  548. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  549. * that needs to be released.
  550. */
  551. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  552. {
  553. int ret;
  554. ret = devres_release(dev, devm_nvmem_device_release,
  555. devm_nvmem_device_match, nvmem);
  556. WARN_ON(ret);
  557. }
  558. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  559. /**
  560. * nvmem_device_put() - put alredy got nvmem device
  561. *
  562. * @nvmem: pointer to nvmem device that needs to be released.
  563. */
  564. void nvmem_device_put(struct nvmem_device *nvmem)
  565. {
  566. __nvmem_device_put(nvmem);
  567. }
  568. EXPORT_SYMBOL_GPL(nvmem_device_put);
  569. /**
  570. * devm_nvmem_device_get() - Get nvmem cell of device form a given id
  571. *
  572. * @dev: Device that requests the nvmem device.
  573. * @id: name id for the requested nvmem device.
  574. *
  575. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
  576. * on success. The nvmem_cell will be freed by the automatically once the
  577. * device is freed.
  578. */
  579. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  580. {
  581. struct nvmem_device **ptr, *nvmem;
  582. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  583. if (!ptr)
  584. return ERR_PTR(-ENOMEM);
  585. nvmem = nvmem_device_get(dev, id);
  586. if (!IS_ERR(nvmem)) {
  587. *ptr = nvmem;
  588. devres_add(dev, ptr);
  589. } else {
  590. devres_free(ptr);
  591. }
  592. return nvmem;
  593. }
  594. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  595. static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id)
  596. {
  597. struct nvmem_cell *cell = NULL;
  598. struct nvmem_device *nvmem;
  599. nvmem = __nvmem_device_get(NULL, &cell, cell_id);
  600. if (IS_ERR(nvmem))
  601. return ERR_CAST(nvmem);
  602. return cell;
  603. }
  604. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  605. /**
  606. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  607. *
  608. * @np: Device tree node that uses the nvmem cell.
  609. * @name: nvmem cell name from nvmem-cell-names property, or NULL
  610. * for the cell at index 0 (the lone cell with no accompanying
  611. * nvmem-cell-names property).
  612. *
  613. * Return: Will be an ERR_PTR() on error or a valid pointer
  614. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  615. * nvmem_cell_put().
  616. */
  617. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
  618. const char *name)
  619. {
  620. struct device_node *cell_np, *nvmem_np;
  621. struct nvmem_cell *cell;
  622. struct nvmem_device *nvmem;
  623. const __be32 *addr;
  624. int rval, len;
  625. int index = 0;
  626. /* if cell name exists, find index to the name */
  627. if (name)
  628. index = of_property_match_string(np, "nvmem-cell-names", name);
  629. cell_np = of_parse_phandle(np, "nvmem-cells", index);
  630. if (!cell_np)
  631. return ERR_PTR(-EINVAL);
  632. nvmem_np = of_get_next_parent(cell_np);
  633. if (!nvmem_np)
  634. return ERR_PTR(-EINVAL);
  635. nvmem = __nvmem_device_get(nvmem_np, NULL, NULL);
  636. if (IS_ERR(nvmem))
  637. return ERR_CAST(nvmem);
  638. addr = of_get_property(cell_np, "reg", &len);
  639. if (!addr || (len < 2 * sizeof(u32))) {
  640. dev_err(&nvmem->dev, "nvmem: invalid reg on %s\n",
  641. cell_np->full_name);
  642. rval = -EINVAL;
  643. goto err_mem;
  644. }
  645. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  646. if (!cell) {
  647. rval = -ENOMEM;
  648. goto err_mem;
  649. }
  650. cell->nvmem = nvmem;
  651. cell->offset = be32_to_cpup(addr++);
  652. cell->bytes = be32_to_cpup(addr);
  653. cell->name = cell_np->name;
  654. addr = of_get_property(cell_np, "bits", &len);
  655. if (addr && len == (2 * sizeof(u32))) {
  656. cell->bit_offset = be32_to_cpup(addr++);
  657. cell->nbits = be32_to_cpup(addr);
  658. }
  659. if (cell->nbits)
  660. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  661. BITS_PER_BYTE);
  662. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  663. dev_err(&nvmem->dev,
  664. "cell %s unaligned to nvmem stride %d\n",
  665. cell->name, nvmem->stride);
  666. rval = -EINVAL;
  667. goto err_sanity;
  668. }
  669. nvmem_cell_add(cell);
  670. return cell;
  671. err_sanity:
  672. kfree(cell);
  673. err_mem:
  674. __nvmem_device_put(nvmem);
  675. return ERR_PTR(rval);
  676. }
  677. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  678. #endif
  679. /**
  680. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  681. *
  682. * @dev: Device that requests the nvmem cell.
  683. * @cell_id: nvmem cell name to get.
  684. *
  685. * Return: Will be an ERR_PTR() on error or a valid pointer
  686. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  687. * nvmem_cell_put().
  688. */
  689. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id)
  690. {
  691. struct nvmem_cell *cell;
  692. if (dev->of_node) { /* try dt first */
  693. cell = of_nvmem_cell_get(dev->of_node, cell_id);
  694. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  695. return cell;
  696. }
  697. return nvmem_cell_get_from_list(cell_id);
  698. }
  699. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  700. static void devm_nvmem_cell_release(struct device *dev, void *res)
  701. {
  702. nvmem_cell_put(*(struct nvmem_cell **)res);
  703. }
  704. /**
  705. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  706. *
  707. * @dev: Device that requests the nvmem cell.
  708. * @id: nvmem cell name id to get.
  709. *
  710. * Return: Will be an ERR_PTR() on error or a valid pointer
  711. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  712. * automatically once the device is freed.
  713. */
  714. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  715. {
  716. struct nvmem_cell **ptr, *cell;
  717. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  718. if (!ptr)
  719. return ERR_PTR(-ENOMEM);
  720. cell = nvmem_cell_get(dev, id);
  721. if (!IS_ERR(cell)) {
  722. *ptr = cell;
  723. devres_add(dev, ptr);
  724. } else {
  725. devres_free(ptr);
  726. }
  727. return cell;
  728. }
  729. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  730. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  731. {
  732. struct nvmem_cell **c = res;
  733. if (WARN_ON(!c || !*c))
  734. return 0;
  735. return *c == data;
  736. }
  737. /**
  738. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  739. * from devm_nvmem_cell_get.
  740. *
  741. * @dev: Device that requests the nvmem cell.
  742. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
  743. */
  744. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  745. {
  746. int ret;
  747. ret = devres_release(dev, devm_nvmem_cell_release,
  748. devm_nvmem_cell_match, cell);
  749. WARN_ON(ret);
  750. }
  751. EXPORT_SYMBOL(devm_nvmem_cell_put);
  752. /**
  753. * nvmem_cell_put() - Release previously allocated nvmem cell.
  754. *
  755. * @cell: Previously allocated nvmem cell by nvmem_cell_get().
  756. */
  757. void nvmem_cell_put(struct nvmem_cell *cell)
  758. {
  759. struct nvmem_device *nvmem = cell->nvmem;
  760. __nvmem_device_put(nvmem);
  761. nvmem_cell_drop(cell);
  762. }
  763. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  764. static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell,
  765. void *buf)
  766. {
  767. u8 *p, *b;
  768. int i, bit_offset = cell->bit_offset;
  769. p = b = buf;
  770. if (bit_offset) {
  771. /* First shift */
  772. *b++ >>= bit_offset;
  773. /* setup rest of the bytes if any */
  774. for (i = 1; i < cell->bytes; i++) {
  775. /* Get bits from next byte and shift them towards msb */
  776. *p |= *b << (BITS_PER_BYTE - bit_offset);
  777. p = b;
  778. *b++ >>= bit_offset;
  779. }
  780. /* result fits in less bytes */
  781. if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
  782. *p-- = 0;
  783. }
  784. /* clear msb bits if any leftover in the last byte */
  785. *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
  786. }
  787. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  788. struct nvmem_cell *cell,
  789. void *buf, size_t *len)
  790. {
  791. int rc;
  792. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
  793. if (rc)
  794. return rc;
  795. /* shift bits in-place */
  796. if (cell->bit_offset || cell->nbits)
  797. nvmem_shift_read_buffer_in_place(cell, buf);
  798. if (len)
  799. *len = cell->bytes;
  800. return 0;
  801. }
  802. /**
  803. * nvmem_cell_read() - Read a given nvmem cell
  804. *
  805. * @cell: nvmem cell to be read.
  806. * @len: pointer to length of cell which will be populated on successful read;
  807. * can be NULL.
  808. *
  809. * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
  810. * buffer should be freed by the consumer with a kfree().
  811. */
  812. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  813. {
  814. struct nvmem_device *nvmem = cell->nvmem;
  815. u8 *buf;
  816. int rc;
  817. if (!nvmem)
  818. return ERR_PTR(-EINVAL);
  819. buf = kzalloc(cell->bytes, GFP_KERNEL);
  820. if (!buf)
  821. return ERR_PTR(-ENOMEM);
  822. rc = __nvmem_cell_read(nvmem, cell, buf, len);
  823. if (rc) {
  824. kfree(buf);
  825. return ERR_PTR(rc);
  826. }
  827. return buf;
  828. }
  829. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  830. static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
  831. u8 *_buf, int len)
  832. {
  833. struct nvmem_device *nvmem = cell->nvmem;
  834. int i, rc, nbits, bit_offset = cell->bit_offset;
  835. u8 v, *p, *buf, *b, pbyte, pbits;
  836. nbits = cell->nbits;
  837. buf = kzalloc(cell->bytes, GFP_KERNEL);
  838. if (!buf)
  839. return ERR_PTR(-ENOMEM);
  840. memcpy(buf, _buf, len);
  841. p = b = buf;
  842. if (bit_offset) {
  843. pbyte = *b;
  844. *b <<= bit_offset;
  845. /* setup the first byte with lsb bits from nvmem */
  846. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  847. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  848. /* setup rest of the byte if any */
  849. for (i = 1; i < cell->bytes; i++) {
  850. /* Get last byte bits and shift them towards lsb */
  851. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  852. pbyte = *b;
  853. p = b;
  854. *b <<= bit_offset;
  855. *b++ |= pbits;
  856. }
  857. }
  858. /* if it's not end on byte boundary */
  859. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  860. /* setup the last byte with msb bits from nvmem */
  861. rc = nvmem_reg_read(nvmem,
  862. cell->offset + cell->bytes - 1, &v, 1);
  863. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  864. }
  865. return buf;
  866. }
  867. /**
  868. * nvmem_cell_write() - Write to a given nvmem cell
  869. *
  870. * @cell: nvmem cell to be written.
  871. * @buf: Buffer to be written.
  872. * @len: length of buffer to be written to nvmem cell.
  873. *
  874. * Return: length of bytes written or negative on failure.
  875. */
  876. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  877. {
  878. struct nvmem_device *nvmem = cell->nvmem;
  879. int rc;
  880. if (!nvmem || nvmem->read_only ||
  881. (cell->bit_offset == 0 && len != cell->bytes))
  882. return -EINVAL;
  883. if (cell->bit_offset || cell->nbits) {
  884. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  885. if (IS_ERR(buf))
  886. return PTR_ERR(buf);
  887. }
  888. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  889. /* free the tmp buffer */
  890. if (cell->bit_offset || cell->nbits)
  891. kfree(buf);
  892. if (rc)
  893. return rc;
  894. return len;
  895. }
  896. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  897. /**
  898. * nvmem_device_cell_read() - Read a given nvmem device and cell
  899. *
  900. * @nvmem: nvmem device to read from.
  901. * @info: nvmem cell info to be read.
  902. * @buf: buffer pointer which will be populated on successful read.
  903. *
  904. * Return: length of successful bytes read on success and negative
  905. * error code on error.
  906. */
  907. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  908. struct nvmem_cell_info *info, void *buf)
  909. {
  910. struct nvmem_cell cell;
  911. int rc;
  912. ssize_t len;
  913. if (!nvmem)
  914. return -EINVAL;
  915. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  916. if (rc)
  917. return rc;
  918. rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
  919. if (rc)
  920. return rc;
  921. return len;
  922. }
  923. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  924. /**
  925. * nvmem_device_cell_write() - Write cell to a given nvmem device
  926. *
  927. * @nvmem: nvmem device to be written to.
  928. * @info: nvmem cell info to be written.
  929. * @buf: buffer to be written to cell.
  930. *
  931. * Return: length of bytes written or negative error code on failure.
  932. * */
  933. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  934. struct nvmem_cell_info *info, void *buf)
  935. {
  936. struct nvmem_cell cell;
  937. int rc;
  938. if (!nvmem)
  939. return -EINVAL;
  940. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  941. if (rc)
  942. return rc;
  943. return nvmem_cell_write(&cell, buf, cell.bytes);
  944. }
  945. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  946. /**
  947. * nvmem_device_read() - Read from a given nvmem device
  948. *
  949. * @nvmem: nvmem device to read from.
  950. * @offset: offset in nvmem device.
  951. * @bytes: number of bytes to read.
  952. * @buf: buffer pointer which will be populated on successful read.
  953. *
  954. * Return: length of successful bytes read on success and negative
  955. * error code on error.
  956. */
  957. int nvmem_device_read(struct nvmem_device *nvmem,
  958. unsigned int offset,
  959. size_t bytes, void *buf)
  960. {
  961. int rc;
  962. if (!nvmem)
  963. return -EINVAL;
  964. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  965. if (rc)
  966. return rc;
  967. return bytes;
  968. }
  969. EXPORT_SYMBOL_GPL(nvmem_device_read);
  970. /**
  971. * nvmem_device_write() - Write cell to a given nvmem device
  972. *
  973. * @nvmem: nvmem device to be written to.
  974. * @offset: offset in nvmem device.
  975. * @bytes: number of bytes to write.
  976. * @buf: buffer to be written.
  977. *
  978. * Return: length of bytes written or negative error code on failure.
  979. * */
  980. int nvmem_device_write(struct nvmem_device *nvmem,
  981. unsigned int offset,
  982. size_t bytes, void *buf)
  983. {
  984. int rc;
  985. if (!nvmem)
  986. return -EINVAL;
  987. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  988. if (rc)
  989. return rc;
  990. return bytes;
  991. }
  992. EXPORT_SYMBOL_GPL(nvmem_device_write);
  993. static int __init nvmem_init(void)
  994. {
  995. return bus_register(&nvmem_bus_type);
  996. }
  997. static void __exit nvmem_exit(void)
  998. {
  999. bus_unregister(&nvmem_bus_type);
  1000. }
  1001. subsys_initcall(nvmem_init);
  1002. module_exit(nvmem_exit);
  1003. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  1004. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  1005. MODULE_DESCRIPTION("nvmem Driver Core");
  1006. MODULE_LICENSE("GPL v2");