xref: /linux/arch/powerpc/platforms/powernv/opal-sensor.c (revision e3c5c2e0bc857c7cbdb81f35a6b90236c5f330e9)
17224adbbSNeelesh Gupta /*
27224adbbSNeelesh Gupta  * PowerNV sensor code
37224adbbSNeelesh Gupta  *
47224adbbSNeelesh Gupta  * Copyright (C) 2013 IBM
57224adbbSNeelesh Gupta  *
67224adbbSNeelesh Gupta  * This program is free software; you can redistribute it and/or modify
77224adbbSNeelesh Gupta  * it under the terms of the GNU General Public License as published by
87224adbbSNeelesh Gupta  * the Free Software Foundation; either version 2 of the License, or
97224adbbSNeelesh Gupta  * (at your option) any later version.
107224adbbSNeelesh Gupta  *
117224adbbSNeelesh Gupta  * This program is distributed in the hope that it will be useful,
127224adbbSNeelesh Gupta  * but WITHOUT ANY WARRANTY; without even the implied warranty of
137224adbbSNeelesh Gupta  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147224adbbSNeelesh Gupta  * GNU General Public License for more details.
157224adbbSNeelesh Gupta  *
167224adbbSNeelesh Gupta  * You should have received a copy of the GNU General Public License
177224adbbSNeelesh Gupta  * along with this program; if not, write to the Free Software
187224adbbSNeelesh Gupta  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
197224adbbSNeelesh Gupta  */
207224adbbSNeelesh Gupta 
217224adbbSNeelesh Gupta #include <linux/delay.h>
227224adbbSNeelesh Gupta #include <linux/mutex.h>
238de303baSNeelesh Gupta #include <linux/of_platform.h>
247224adbbSNeelesh Gupta #include <asm/opal.h>
258de303baSNeelesh Gupta #include <asm/machdep.h>
267224adbbSNeelesh Gupta 
277224adbbSNeelesh Gupta static DEFINE_MUTEX(opal_sensor_mutex);
287224adbbSNeelesh Gupta 
297224adbbSNeelesh Gupta /*
307224adbbSNeelesh Gupta  * This will return sensor information to driver based on the requested sensor
317224adbbSNeelesh Gupta  * handle. A handle is an opaque id for the powernv, read by the driver from the
327224adbbSNeelesh Gupta  * device tree..
337224adbbSNeelesh Gupta  */
347224adbbSNeelesh Gupta int opal_get_sensor_data(u32 sensor_hndl, u32 *sensor_data)
357224adbbSNeelesh Gupta {
367224adbbSNeelesh Gupta 	int ret, token;
377224adbbSNeelesh Gupta 	struct opal_msg msg;
389000c17dSAnton Blanchard 	__be32 data;
397224adbbSNeelesh Gupta 
407224adbbSNeelesh Gupta 	token = opal_async_get_token_interruptible();
417224adbbSNeelesh Gupta 	if (token < 0) {
427224adbbSNeelesh Gupta 		pr_err("%s: Couldn't get the token, returning\n", __func__);
437224adbbSNeelesh Gupta 		ret = token;
447224adbbSNeelesh Gupta 		goto out;
457224adbbSNeelesh Gupta 	}
467224adbbSNeelesh Gupta 
477224adbbSNeelesh Gupta 	mutex_lock(&opal_sensor_mutex);
489000c17dSAnton Blanchard 	ret = opal_sensor_read(sensor_hndl, token, &data);
49*e3c5c2e0SCédric Le Goater 	if (ret != OPAL_ASYNC_COMPLETION) {
50*e3c5c2e0SCédric Le Goater 		ret = opal_error_code(ret);
517224adbbSNeelesh Gupta 		goto out_token;
52*e3c5c2e0SCédric Le Goater 	}
537224adbbSNeelesh Gupta 
547224adbbSNeelesh Gupta 	ret = opal_async_wait_response(token, &msg);
557224adbbSNeelesh Gupta 	if (ret) {
567224adbbSNeelesh Gupta 		pr_err("%s: Failed to wait for the async response, %d\n",
577224adbbSNeelesh Gupta 				__func__, ret);
587224adbbSNeelesh Gupta 		goto out_token;
597224adbbSNeelesh Gupta 	}
607224adbbSNeelesh Gupta 
619000c17dSAnton Blanchard 	*sensor_data = be32_to_cpu(data);
62*e3c5c2e0SCédric Le Goater 	ret = opal_error_code(be64_to_cpu(msg.params[1]));
637224adbbSNeelesh Gupta 
647224adbbSNeelesh Gupta out_token:
657224adbbSNeelesh Gupta 	mutex_unlock(&opal_sensor_mutex);
667224adbbSNeelesh Gupta 	opal_async_release_token(token);
677224adbbSNeelesh Gupta out:
687224adbbSNeelesh Gupta 	return ret;
697224adbbSNeelesh Gupta }
707224adbbSNeelesh Gupta EXPORT_SYMBOL_GPL(opal_get_sensor_data);
718de303baSNeelesh Gupta 
728de303baSNeelesh Gupta static __init int opal_sensor_init(void)
738de303baSNeelesh Gupta {
748de303baSNeelesh Gupta 	struct platform_device *pdev;
758de303baSNeelesh Gupta 	struct device_node *sensor;
768de303baSNeelesh Gupta 
778de303baSNeelesh Gupta 	sensor = of_find_node_by_path("/ibm,opal/sensors");
788de303baSNeelesh Gupta 	if (!sensor) {
798de303baSNeelesh Gupta 		pr_err("Opal node 'sensors' not found\n");
808de303baSNeelesh Gupta 		return -ENODEV;
818de303baSNeelesh Gupta 	}
828de303baSNeelesh Gupta 
838de303baSNeelesh Gupta 	pdev = of_platform_device_create(sensor, "opal-sensor", NULL);
848de303baSNeelesh Gupta 	of_node_put(sensor);
858de303baSNeelesh Gupta 
868de303baSNeelesh Gupta 	return PTR_ERR_OR_ZERO(pdev);
878de303baSNeelesh Gupta }
888de303baSNeelesh Gupta machine_subsys_initcall(powernv, opal_sensor_init);
89