Documentation / i2c / upgrading-clients.rst


Based on kernel version 5.8. Page generated on 2020-08-08 17:40 EST.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
=================================================
Upgrading I2C Drivers to the new 2.6 Driver Model
=================================================

Ben Dooks <ben-linux@fluff.org>

Introduction
------------

This guide outlines how to alter existing Linux 2.6 client drivers from
the old to the new new binding methods.


Example old-style driver
------------------------

::

  struct example_state {
	struct i2c_client	client;
	....
  };

  static struct i2c_driver example_driver;

  static unsigned short ignore[] = { I2C_CLIENT_END };
  static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };

  I2C_CLIENT_INSMOD;

  static int example_attach(struct i2c_adapter *adap, int addr, int kind)
  {
	struct example_state *state;
	struct device *dev = &adap->dev;  /* to use for dev_ reports */
	int ret;

	state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
	if (state == NULL) {
		dev_err(dev, "failed to create our state\n");
		return -ENOMEM;
	}

	example->client.addr    = addr;
	example->client.flags   = 0;
	example->client.adapter = adap;

	i2c_set_clientdata(&state->i2c_client, state);
	strscpy(client->i2c_client.name, "example", sizeof(client->i2c_client.name));

	ret = i2c_attach_client(&state->i2c_client);
	if (ret < 0) {
		dev_err(dev, "failed to attach client\n");
		kfree(state);
		return ret;
	}

	dev = &state->i2c_client.dev;

	/* rest of the initialisation goes here. */

	dev_info(dev, "example client created\n");

	return 0;
  }

  static int example_detach(struct i2c_client *client)
  {
	struct example_state *state = i2c_get_clientdata(client);

	i2c_detach_client(client);
	kfree(state);
	return 0;
  }

  static int example_attach_adapter(struct i2c_adapter *adap)
  {
	return i2c_probe(adap, &addr_data, example_attach);
  }

  static struct i2c_driver example_driver = {
	.driver		= {
		.owner		= THIS_MODULE,
		.name		= "example",
		.pm		= &example_pm_ops,
	},
	.attach_adapter = example_attach_adapter,
	.detach_client	= example_detach,
  };


Updating the client
-------------------

The new style binding model will check against a list of supported
devices and their associated address supplied by the code registering
the busses. This means that the driver .attach_adapter and
.detach_client methods can be removed, along with the addr_data,
as follows::

  - static struct i2c_driver example_driver;

  - static unsigned short ignore[] = { I2C_CLIENT_END };
  - static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };

  - I2C_CLIENT_INSMOD;

  - static int example_attach_adapter(struct i2c_adapter *adap)
  - {
  - 	return i2c_probe(adap, &addr_data, example_attach);
  - }

    static struct i2c_driver example_driver = {
  -	.attach_adapter = example_attach_adapter,
  -	.detach_client	= example_detach,
    }

Add the probe and remove methods to the i2c_driver, as so::

   static struct i2c_driver example_driver = {
  +	.probe		= example_probe,
  +	.remove		= example_remove,
   }

Change the example_attach method to accept the new parameters
which include the i2c_client that it will be working with::

  - static int example_attach(struct i2c_adapter *adap, int addr, int kind)
  + static int example_probe(struct i2c_client *client,
  +			   const struct i2c_device_id *id)

Change the name of example_attach to example_probe to align it with the
i2c_driver entry names. The rest of the probe routine will now need to be
changed as the i2c_client has already been setup for use.

The necessary client fields have already been setup before
the probe function is called, so the following client setup
can be removed::

  -	example->client.addr    = addr;
  -	example->client.flags   = 0;
  -	example->client.adapter = adap;
  -
  -	strscpy(client->i2c_client.name, "example", sizeof(client->i2c_client.name));

The i2c_set_clientdata is now::

  -	i2c_set_clientdata(&state->client, state);
  +	i2c_set_clientdata(client, state);

The call to i2c_attach_client is no longer needed, if the probe
routine exits successfully, then the driver will be automatically
attached by the core. Change the probe routine as so::

  -	ret = i2c_attach_client(&state->i2c_client);
  -	if (ret < 0) {
  -		dev_err(dev, "failed to attach client\n");
  -		kfree(state);
  -		return ret;
  -	}


Remove the storage of 'struct i2c_client' from the 'struct example_state'
as we are provided with the i2c_client in our example_probe. Instead we
store a pointer to it for when it is needed.

::

  struct example_state {
  -	struct i2c_client	client;
  +	struct i2c_client	*client;

the new i2c client as so::

  -	struct device *dev = &adap->dev;  /* to use for dev_ reports */
  + 	struct device *dev = &i2c_client->dev;  /* to use for dev_ reports */

And remove the change after our client is attached, as the driver no
longer needs to register a new client structure with the core::

  -	dev = &state->i2c_client.dev;

In the probe routine, ensure that the new state has the client stored
in it::

  static int example_probe(struct i2c_client *i2c_client,
			 const struct i2c_device_id *id)
  {
	struct example_state *state;
	struct device *dev = &i2c_client->dev;
	int ret;

	state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
	if (state == NULL) {
		dev_err(dev, "failed to create our state\n");
		return -ENOMEM;
	}

  +	state->client = i2c_client;

Update the detach method, by changing the name to _remove and
to delete the i2c_detach_client call. It is possible that you
can also remove the ret variable as it is not needed for any
of the core functions.

::

  - static int example_detach(struct i2c_client *client)
  + static int example_remove(struct i2c_client *client)
  {
	struct example_state *state = i2c_get_clientdata(client);

  -	i2c_detach_client(client);

And finally ensure that we have the correct ID table for the i2c-core
and other utilities::

  + struct i2c_device_id example_idtable[] = {
  +       { "example", 0 },
  +       { }
  +};
  +
  +MODULE_DEVICE_TABLE(i2c, example_idtable);

  static struct i2c_driver example_driver = {
	.driver		= {
		.owner		= THIS_MODULE,
		.name		= "example",
	},
  +	.id_table	= example_ids,


Our driver should now look like this::

  struct example_state {
	struct i2c_client	*client;
	....
  };

  static int example_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
  {
	struct example_state *state;
	struct device *dev = &client->dev;

	state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
	if (state == NULL) {
		dev_err(dev, "failed to create our state\n");
		return -ENOMEM;
	}

	state->client = client;
	i2c_set_clientdata(client, state);

	/* rest of the initialisation goes here. */

	dev_info(dev, "example client created\n");

	return 0;
  }

  static int example_remove(struct i2c_client *client)
  {
	struct example_state *state = i2c_get_clientdata(client);

	kfree(state);
	return 0;
  }

  static struct i2c_device_id example_idtable[] = {
	{ "example", 0 },
	{ }
  };

  MODULE_DEVICE_TABLE(i2c, example_idtable);

  static struct i2c_driver example_driver = {
	.driver		= {
		.owner		= THIS_MODULE,
		.name		= "example",
		.pm		= &example_pm_ops,
	},
	.id_table	= example_idtable,
	.probe		= example_probe,
	.remove		= example_remove,
  };