xref: /linux/drivers/media/usb/dvb-usb/dvb-usb-i2c.c (revision c0decac19da3906d9b66291e57b7759489e1170f)
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 
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");
20786baecfSMauro Carvalho Chehab 		return -EINVAL;
21786baecfSMauro Carvalho Chehab 	}
22786baecfSMauro Carvalho Chehab 
23*c0decac1SMauro Carvalho Chehab 	strscpy(d->i2c_adap.name, d->desc->name, sizeof(d->i2c_adap.name));
24786baecfSMauro Carvalho Chehab 	d->i2c_adap.algo      = d->props.i2c_algo;
25786baecfSMauro Carvalho Chehab 	d->i2c_adap.algo_data = NULL;
26786baecfSMauro Carvalho Chehab 	d->i2c_adap.dev.parent = &d->udev->dev;
27786baecfSMauro Carvalho Chehab 
28786baecfSMauro Carvalho Chehab 	i2c_set_adapdata(&d->i2c_adap, d);
29786baecfSMauro Carvalho Chehab 
30786baecfSMauro Carvalho Chehab 	if ((ret = i2c_add_adapter(&d->i2c_adap)) < 0)
31786baecfSMauro Carvalho Chehab 		err("could not add i2c adapter");
32786baecfSMauro Carvalho Chehab 
33786baecfSMauro Carvalho Chehab 	d->state |= DVB_USB_STATE_I2C;
34786baecfSMauro Carvalho Chehab 
35786baecfSMauro Carvalho Chehab 	return ret;
36786baecfSMauro Carvalho Chehab }
37786baecfSMauro Carvalho Chehab 
38786baecfSMauro Carvalho Chehab int dvb_usb_i2c_exit(struct dvb_usb_device *d)
39786baecfSMauro Carvalho Chehab {
40786baecfSMauro Carvalho Chehab 	if (d->state & DVB_USB_STATE_I2C)
41786baecfSMauro Carvalho Chehab 		i2c_del_adapter(&d->i2c_adap);
42786baecfSMauro Carvalho Chehab 	d->state &= ~DVB_USB_STATE_I2C;
43786baecfSMauro Carvalho Chehab 	return 0;
44786baecfSMauro Carvalho Chehab }
45