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