blob: 3919aad3c8af8c96a54e9aa05c91906845b04fe5 [file] [log] [blame]
Maxime Ripardf674fc02018-09-18 10:35:27 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 *
4 * Copyright (c) 2015 Free Electrons
5 * Copyright (c) 2015 NextThing Co.
6 * Copyright (c) 2018 Microchip Technology, Inc.
7 *
8 * Maxime Ripard <maxime.ripard@free-electrons.com>
9 * Eugen Hristev <eugen.hristev@microchip.com>
10 *
11 */
12
Patrick Delaunay81313352021-04-27 11:02:19 +020013#define LOG_CATEGORY UCLASS_W1_EEPROM
14
Maxime Ripardf674fc02018-09-18 10:35:27 +030015#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Maxime Ripardf674fc02018-09-18 10:35:27 +030017#include <w1.h>
18#include <w1-eeprom.h>
19
20#include <dm/device-internal.h>
21
22int w1_eeprom_read_buf(struct udevice *dev, unsigned int offset,
23 u8 *buf, unsigned int count)
24{
25 const struct w1_eeprom_ops *ops = device_get_ops(dev);
26 u64 id = 0;
27 int ret;
28
29 if (!ops->read_buf)
30 return -ENOSYS;
31
32 ret = w1_eeprom_get_id(dev, &id);
33 if (ret)
34 return ret;
35 if (!id)
36 return -ENODEV;
37
38 return ops->read_buf(dev, offset, buf, count);
39}
40
Maxime Ripardf674fc02018-09-18 10:35:27 +030041int w1_eeprom_get_id(struct udevice *dev, u64 *id)
42{
Simon Glass71fa5b42020-12-03 16:55:18 -070043 struct w1_device *w1 = dev_get_parent_plat(dev);
Maxime Ripardf674fc02018-09-18 10:35:27 +030044
45 if (!w1)
46 return -ENODEV;
47 *id = w1->id;
48
49 return 0;
50}
51
52UCLASS_DRIVER(w1_eeprom) = {
53 .name = "w1_eeprom",
54 .id = UCLASS_W1_EEPROM,
55 .flags = DM_UC_FLAG_SEQ_ALIAS,
56#if CONFIG_IS_ENABLED(OF_CONTROL)
57 .post_bind = dm_scan_fdt_dev,
58#endif
59};
60
61int w1_eeprom_dm_init(void)
62{
63 struct udevice *dev;
64 struct uclass *uc;
65 int ret;
66
67 ret = uclass_get(UCLASS_W1_EEPROM, &uc);
68 if (ret) {
69 debug("W1_EEPROM uclass not available\n");
70 return ret;
71 }
72
73 uclass_foreach_dev(dev, uc) {
74 ret = device_probe(dev);
75 if (ret == -ENODEV) { /* No such device. */
76 debug("W1_EEPROM not available.\n");
77 continue;
78 }
79
80 if (ret) { /* Other error. */
81 printf("W1_EEPROM probe failed, error %d\n", ret);
82 continue;
83 }
84 }
85
86 return 0;
87}