xref: /linux/drivers/media/usb/dvb-usb/dvb-usb-i2c.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2786baecfSMauro Carvalho Chehab /* dvb-usb-i2c.c is part of the DVB USB library.
3786baecfSMauro Carvalho Chehab  *
499e44da7SPatrick Boettcher  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
5786baecfSMauro Carvalho Chehab  * see dvb-usb-init.c for copyright information.
6786baecfSMauro Carvalho Chehab  *
7786baecfSMauro Carvalho Chehab  * This file contains functions for (de-)initializing an I2C adapter.
8786baecfSMauro Carvalho Chehab  */
9786baecfSMauro Carvalho Chehab #include "dvb-usb-common.h"
10786baecfSMauro Carvalho Chehab 
dvb_usb_i2c_init(struct dvb_usb_device * d)11786baecfSMauro Carvalho Chehab int dvb_usb_i2c_init(struct dvb_usb_device *d)
12786baecfSMauro Carvalho Chehab {
13786baecfSMauro Carvalho Chehab 	int ret = 0;
14786baecfSMauro Carvalho Chehab 
15786baecfSMauro Carvalho Chehab 	if (!(d->props.caps & DVB_USB_IS_AN_I2C_ADAPTER))
16786baecfSMauro Carvalho Chehab 		return 0;
17786baecfSMauro Carvalho Chehab 
18786baecfSMauro Carvalho Chehab 	if (d->props.i2c_algo == NULL) {
19786baecfSMauro Carvalho Chehab 		err("no i2c algorithm specified");
20*131ae388SDongliang Mu 		ret = -EINVAL;
21*131ae388SDongliang Mu 		goto err;
22786baecfSMauro Carvalho Chehab 	}
23786baecfSMauro Carvalho Chehab 
24c0decac1SMauro Carvalho Chehab 	strscpy(d->i2c_adap.name, d->desc->name, sizeof(d->i2c_adap.name));
25786baecfSMauro Carvalho Chehab 	d->i2c_adap.algo      = d->props.i2c_algo;
26786baecfSMauro Carvalho Chehab 	d->i2c_adap.algo_data = NULL;
27786baecfSMauro Carvalho Chehab 	d->i2c_adap.dev.parent = &d->udev->dev;
28786baecfSMauro Carvalho Chehab 
29786baecfSMauro Carvalho Chehab 	i2c_set_adapdata(&d->i2c_adap, d);
30786baecfSMauro Carvalho Chehab 
31*131ae388SDongliang Mu 	ret = i2c_add_adapter(&d->i2c_adap);
32*131ae388SDongliang Mu 	if (ret < 0) {
33786baecfSMauro Carvalho Chehab 		err("could not add i2c adapter");
34*131ae388SDongliang Mu 		goto err;
35*131ae388SDongliang Mu 	}
36786baecfSMauro Carvalho Chehab 
37786baecfSMauro Carvalho Chehab 	d->state |= DVB_USB_STATE_I2C;
38786baecfSMauro Carvalho Chehab 
39*131ae388SDongliang Mu err:
40786baecfSMauro Carvalho Chehab 	return ret;
41786baecfSMauro Carvalho Chehab }
42786baecfSMauro Carvalho Chehab 
dvb_usb_i2c_exit(struct dvb_usb_device * d)43786baecfSMauro Carvalho Chehab int dvb_usb_i2c_exit(struct dvb_usb_device *d)
44786baecfSMauro Carvalho Chehab {
45786baecfSMauro Carvalho Chehab 	if (d->state & DVB_USB_STATE_I2C)
46786baecfSMauro Carvalho Chehab 		i2c_del_adapter(&d->i2c_adap);
47786baecfSMauro Carvalho Chehab 	d->state &= ~DVB_USB_STATE_I2C;
48786baecfSMauro Carvalho Chehab 	return 0;
49786baecfSMauro Carvalho Chehab }
50