Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MAX30210 #2544

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jemfgeronimo
Copy link

@jemfgeronimo jemfgeronimo commented Jul 11, 2024

PR Description

  • This adds driver support, device tree bindings, and ABI documentation for added attributes for MAX30210 Temperature Sensor.
  • Datasheet: MAX30210
  • Tested on RPI4 with MAX30210EVKIT
  • Driver was initially done by Daniel Matyas: 8fc25e1
    • I've added features such as added events and attributes related to temperature rate-of-change
    • From what I've understood from his source code, I've also changed the trigger source of the triggered buffer from CVT/PDB pin to INT pin.

PR Type

  • Bug fix (a change that fixes an issue)
  • New feature (a change that adds new functionality)
  • Breaking change (a change that affects other repos or cause CIs to fail)

PR Checklist

  • I have conducted a self-review of my own code changes
  • I have tested the changes on the relevant hardware
  • I have updated the documentation outside this repo accordingly (if there is the case)

Copy link
Collaborator

@nunojsa nunojsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I'm done for a first round of reviewing

adi,comp-mode:
description: |
If present, comparator mode is used. If not present, interrupt mode is
used (default).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this property? Can't we assume this mode in the absence of an interrupt?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adapted this from the existing code below:


Comparator mode still uses interrupts. It just uses some register values differently, i.e., low temp alarm is used as high temp hysteresis.
This can also be controlled using debugfs_reg_access; should I just remove it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we can try the DT parameter but I think the description should be improved. More in line with that explanation stating that behavior of the device will change. With the above, it seems that interrupts won't be used at all in comparator mode.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will use the following description:

  If present, the device is in comparator mode. In this mode, the low
  temperature threshold value acts as high temperature hysteresis. If not
  present, interrupt mode is used (default).

drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
Add entry for the MAX30210 driver.

Signed-off-by: John Erasmus Mari Geronimo <[email protected]>
@jemfgeronimo jemfgeronimo force-pushed the dev/max30210 branch 4 times, most recently from 82e4345 to 7a24de3 Compare September 12, 2024 08:58
@jemfgeronimo
Copy link
Author

jemfgeronimo commented Sep 12, 2024

V2

  • Removed math from macros
  • Fixed implementations of events that depends on change detect enable (roc-en) and alert mode (comp-int)
  • Added PWM to implement the multifunction pin CVT/PDB
  • Moved extra attributes to debugfs
  • Made adi,int-output-drive-type into string
  • Fixed indentions
  • Added register reset in probe()
  • Changed fwnode_property to device_property
  • Added powerdown-gpios in dt-bindings
  • Removed roc-filter from dt-bindings
  • Renamed dt property adi,comp-mode to adi,comp-int

Copy link
Collaborator

@nunojsa nunojsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some stuff that still needs to be addressed but I would say that this is already in shape to be sent upstream.

MAINTAINERS Show resolved Hide resolved
adi,comp-mode:
description: |
If present, comparator mode is used. If not present, interrupt mode is
used (default).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we can try the DT parameter but I think the description should be improved. More in line with that explanation stating that behavior of the device will change. With the above, it seems that interrupts won't be used at all in comparator mode.

drivers/iio/temperature/max30210.c Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
if (ret)
return ret;

st->watermark = val;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a lock... Also, should we be allowed to change the watermark while buffering? Would that work out?

Copy link
Author

@jemfgeronimo jemfgeronimo Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, the hwfifo_set_watermark() runs every after preenable of the triggered buffer. I think
st->watermark does not need a lock in this situation.

drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
drivers/iio/temperature/max30210.c Outdated Show resolved Hide resolved
Add documentation for devicetree bindings for max30210

Signed-off-by: John Erasmus Mari Geronimo <[email protected]>
@jemfgeronimo jemfgeronimo force-pushed the dev/max30210 branch 5 times, most recently from 41c93cb to 21ffe8b Compare October 17, 2024 06:14
@jemfgeronimo
Copy link
Author

jemfgeronimo commented Oct 17, 2024

V3

  • removed description of reg property
  • removed adi,int-output-drive-type property
  • removed roc_length from debugfs & add adi,roc-len property
  • improved description of adi,comp-int property
  • removed Daniel Matyas name in MAINTAINERS file
  • added __aligned(IIO_DMA_MINALIGN)
  • added iio_device_claim_direct_mode() in IIO_CHAN_INFO_SAMP_FREQ of max30210_write_raw()
  • improvement of PWM implementation
  • added use of __set_bit()
  • used switch-case instead of if-else in some cases
  • fixed typo of max30210_debugfs_init()

MAX30210 ±0.1°C Accurate Ultra-Small Low-Power Digital Temperature Sensor

Signed-off-by: John Erasmus Mari Geronimo <[email protected]>
static int max30210_read_temp_raw(struct regmap *regmap, unsigned int reg,
int *temp)
{
u8 uval[2] __aligned(IIO_DMA_MINALIGN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to have the cache aligned buffers declared in struct max30210_state?
Most IIO drivers declare their transfer buffers that need to be aligned in their state structs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that. Or can I just remove __aligned(IIO_DMA_MINALIGN)? I honestly don't know the use cases of this alignment. I will appreciate an explanation. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an expert on DMA, but I don't think removing the alignment pragma would be a good thing. I think it is likely that upstream IIO maintainer will ask for explicit buffer alignment to enforce DMA safety. IIRC, the SPI controller in some platforms can talk to the DMA controller and ask SPI transfers to be DMA mapped (rpi can do this IIRC). That would happen seamlessly to the device driver. In those cases, the buffer data can get corrupted if it's not properly aligned on cache lines or miss-handled by the DMA controller. There are other situations in which buffers can get corrupted when data moves along through DMA. Some upstream content about DMA buffer alignment:
https://lore.kernel.org/linux-iio/20240413173409.63d33a0a@jic23-huawei/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
Anyway, I'm not expert on DMA so other reviewers may provide better guided suggestions here.

drivers/iio/temperature/max30210.c Show resolved Hide resolved
static void max30210_fifo_read(struct iio_dev *indio_dev)
{
struct max30210_state *st = iio_priv(indio_dev);
u8 data[3 * MAX30210_FIFO_SIZE] __aligned(IIO_DMA_MINALIGN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this buffer reside in the device state struct?

if (ret)
return dev_err_probe(dev, ret, "Failed to read Part ID.\n");
if (val != MAX30210_PART_ID)
dev_err_probe(dev, -ENODEV, "Part ID mismatch.\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know how maintainers are considering warnings from IIO devices but, not long ago, Greg K-H said to avoid kernel warnings https://www.youtube.com/watch?v=Rg_VPMT0XXw.
In sum, everything that can trigger a dev_warn() would be considered a kernel CVE.
If panic_on_warn is enabled, reboot would happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants