Build-a-Driver #9: Shutdown on .remove

Here's the ninth post in the Build-a-Driver series for MCP9808.
(If this post seems out of context, please refer to "Build-a-Driver Introduction" post.)

MCP9808 powers on in continuous conversion mode.  It is continuously taking temperature measurements.  If the device was to be deleted from the system, without power be removed, it would be nice to go into a low-power mode until/unless the device gets added to the system again.

Note: this won't be suspend/resume operations, but simply shutdown on device deletion, and wake-up on device addition.

From MCP9808 Datasheet:

The driver will execute shutdown mode when a device is deleted.

Example:   # echo  0x50 > /sys/bus/i2c/devices/i2c-3/delete_device

The driver will put the sensor into shutdown/low-power mode.  In shutdown mode, the devices registers are all readable, but it is no longer measuring temperature, hence the alias 'low-power mode'.

Now, once the sensor is put into shutdown mode it stays that way until the bit is cleared via a power-cycle, or, as in this case, it gets probed again. We need to add some initialization and wake-up code to the probe routine to get it back to the expected power-on defaults.  Previously, it was assumed that the sensor was freshly powered-up.  Now, we see that may not always be the case.  

Changes required:

Verified the shutdown by leaving out the probe wake-up changes and confirmed that the registers were all still readable, yet the temperature data was not refreshing. Verified the probe wake-up changes by cycling in/out of shutdown via instantiating new_device/delete_device.

This version mcp9808-shutdown.c
To view complete revision history go here.

Labels: , ,