blob: 7a02af3dd6d35f6f743b4749fe84a71cc51927c7 [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
13#include <common.h>
14#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Maxime Ripardf674fc02018-09-18 10:35:27 +030016#include <w1.h>
17#include <w1-eeprom.h>
18
19#include <dm/device-internal.h>
20
21int w1_eeprom_read_buf(struct udevice *dev, unsigned int offset,
22 u8 *buf, unsigned int count)
23{
24 const struct w1_eeprom_ops *ops = device_get_ops(dev);
25 u64 id = 0;
26 int ret;
27
28 if (!ops->read_buf)
29 return -ENOSYS;
30
31 ret = w1_eeprom_get_id(dev, &id);
32 if (ret)
33 return ret;
34 if (!id)
35 return -ENODEV;
36
37 return ops->read_buf(dev, offset, buf, count);
38}
39
Maxime Ripardf674fc02018-09-18 10:35:27 +030040int w1_eeprom_get_id(struct udevice *dev, u64 *id)
41{
Simon Glass71fa5b42020-12-03 16:55:18 -070042 struct w1_device *w1 = dev_get_parent_plat(dev);
Maxime Ripardf674fc02018-09-18 10:35:27 +030043
44 if (!w1)
45 return -ENODEV;
46 *id = w1->id;
47
48 return 0;
49}
50
51UCLASS_DRIVER(w1_eeprom) = {
52 .name = "w1_eeprom",
53 .id = UCLASS_W1_EEPROM,
54 .flags = DM_UC_FLAG_SEQ_ALIAS,
55#if CONFIG_IS_ENABLED(OF_CONTROL)
56 .post_bind = dm_scan_fdt_dev,
57#endif
58};
59
60int w1_eeprom_dm_init(void)
61{
62 struct udevice *dev;
63 struct uclass *uc;
64 int ret;
65
66 ret = uclass_get(UCLASS_W1_EEPROM, &uc);
67 if (ret) {
68 debug("W1_EEPROM uclass not available\n");
69 return ret;
70 }
71
72 uclass_foreach_dev(dev, uc) {
73 ret = device_probe(dev);
74 if (ret == -ENODEV) { /* No such device. */
75 debug("W1_EEPROM not available.\n");
76 continue;
77 }
78
79 if (ret) { /* Other error. */
80 printf("W1_EEPROM probe failed, error %d\n", ret);
81 continue;
82 }
83 }
84
85 return 0;
86}