1 /*
2  * Copyright (c) 2013 Corey Tabaka
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 
9 #include <lk/err.h>
10 #include <dev/class/i2c.h>
11 
class_i2c_write(struct device * dev,uint8_t addr,const void * buf,size_t len)12 status_t class_i2c_write(struct device *dev, uint8_t addr, const void *buf, size_t len) {
13     struct i2c_ops *ops = device_get_driver_ops(dev, struct i2c_ops, std);
14     if (!ops)
15         return ERR_NOT_CONFIGURED;
16 
17     if (ops->write)
18         return ops->write(dev, addr, buf, len);
19     else
20         return ERR_NOT_SUPPORTED;
21 }
22 
class_i2c_read(struct device * dev,uint8_t addr,void * buf,size_t len)23 status_t class_i2c_read(struct device *dev, uint8_t addr, void *buf, size_t len) {
24     struct i2c_ops *ops = device_get_driver_ops(dev, struct i2c_ops, std);
25     if (!ops)
26         return ERR_NOT_CONFIGURED;
27 
28     if (ops->read)
29         return ops->read(dev, addr, buf, len);
30     else
31         return ERR_NOT_SUPPORTED;
32 }
33 
class_i2c_write_reg(struct device * dev,uint8_t addr,uint8_t reg,uint8_t value)34 status_t class_i2c_write_reg(struct device *dev, uint8_t addr, uint8_t reg, uint8_t value) {
35     struct i2c_ops *ops = device_get_driver_ops(dev, struct i2c_ops, std);
36     if (!ops)
37         return ERR_NOT_CONFIGURED;
38 
39     if (ops->write_reg)
40         return ops->write_reg(dev, addr, reg, value);
41     else
42         return ERR_NOT_SUPPORTED;
43 }
44 
class_i2c_read_reg(struct device * dev,uint8_t addr,uint8_t reg,void * value)45 status_t class_i2c_read_reg(struct device *dev, uint8_t addr, uint8_t reg, void *value) {
46     struct i2c_ops *ops = device_get_driver_ops(dev, struct i2c_ops, std);
47     if (!ops)
48         return ERR_NOT_CONFIGURED;
49 
50     if (ops->read_reg)
51         return ops->read_reg(dev, addr, reg, value);
52     else
53         return ERR_NOT_SUPPORTED;
54 }
55 
56